Examples

team bbl


This section provides some practical examples of regular expressions. Double quotes are used as the delimiter for these examples.

The following regular expression matches any word of normal text:

 "[A-Za-z][a-z-]* " 

The regular expression contains a space at the end, after the *, to indicate the end of the word.

Two regular expressions that match phone numbers are:

 "^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$" 

Matches phone numbers of the format (nnn) nnn-nnnn. Notice the parentheses are escaped. A more flexible regular expression might be:

 "^[0-9)( -]{7,20}$" 

Matches a string that can contain numbers, parentheses, spaces, and dots. The string must be at least 7 characters long but not more than 20 characters long.

To match a zip code, including zip+4, use:

 "^[0-9]{5}(\-[0-9]{4})?$" 

Matches a string of five numbers in the first section ([0-9]{ 5} ). The rest of the regular expression is enclosed in parentheses, making it a single unit that matches the +4 part. A ? after the closing parenthesis makes the entire +4 section optional.

The following regular expression matches a common email address.

 "^.+@.+\.(com|net)$" 

The regular expression includes a literal @ sign. A \ escapes the dot, making it a literal dot. Only .com and .net email addresses are accepted by this regular expression.

    team bbl



    Spring Into Linux
    Spring Into Linux
    ISBN: 0131853546
    EAN: 2147483647
    Year: 2005
    Pages: 362
    Authors: Janet Valade

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net