3.10 Understanding Basic Regular Expression Syntax

 <  Day Day Up  >  

You want to create a regular expression.


Technique

Regular expressions consist of a series of characters and quantifiers on those characters. The characters themselves can be literal or can be denoted by using character classes, such as \d , which denotes a digit character class, or \S , which denotes any nonwhitespace character.

Table 3.4. Regular Expression Single Character Classes

Class

Description

\d

Any digit

\D

Any nondigit

\w s

Any word character

\W

Any nonword character

\s

Any whitespace character

\SW

Any nonwhitespace

In addition to the single character classes, you can also specify a range or set of characters using ranged and set character classes. This ability allows you to narrow the search for a specified character by limiting characters within a specified range or within a defined set.

Table 3.5. Ranged and Set Character Classes

Format

Description

.

Any character except newline.

\p{ uc }

Any character within the Unicode character category uc .

[abcxyz]

Any literal character in the set.

\P{uc}

Any character not within the Unicode character category uc .

[^abcxyz]

Any character not in the set of literal characters.

Quantifiers work on character classes to expand the number of characters the character classes should match. You need to specify, for instance, a wildcard character on a character class, which means 0 or more characters within that class. Additionally, you can also specify a set number of matches of a class that should occur by using an integer within braces following the character class designation.

Table 3.6. Character Class Quantifiers

Format

Description

*

0 or more characters

+

1 or more characters

?

0 or 1 characters

{ n }

Exactly n characters

{ n, }

At least n characters

{ n,m }

At least n but no more than m characters

You can also specify where a certain regular expression should start within a string. Positional assertions allow you to, for instance, match a certain expression as long as it occurs at the beginning or ending of a string. Furthermore, you can create a regular expression that operates on a set of words within a string by using a positional assertion that continues matching on each subsequent word separated by any nonalphanumeric character.

Table 3.7. Positional (Atomic Zero-Width) Assertions

Format

Description

^

Beginning of a string or beginning of a newline

\z

End of the string, including the newline character

$

End of a string before a newline character or at the end of the line

\G

Continues where the last match left off

\A

Beginning of a string

\b

Between word boundaries (between alphanumeric and nonalphanumeric characters)

\Z

End of the string before the newline character

\B

Characters not between word boundaries

Comments

Regular expressions use a variety of characters both symbolic and literal to designate how a particular string of text should be parsed. The act of parsing a string is known as matching, and when applied to a regular expression, the match will be either true or false. In other words, when you use a regular expression to match a series of characters, the match will either succeed or fail. As you can see, this process has powerful applicability in the area of input validation.

You build regular expressions using a series of character classes and quantifiers on those character classes as well as a few miscellaneous regular-expression constructs. You use character classes to match a single character based either on what type of character it is, such as a digit or letter, or whether it belongs within a specified range or set of characters (as shown in Table 3.4). Using this information, you can create a series of character classes to match a certain string of text. For instance, if you want to specify a phone number using character classes, you can use the following regular expression:

 
 \(\d\d\d\)\s\d\d\d-\d\d\d\d 

This expression begins by first escaping the left parenthesis. You must escape it because parentheses are used for grouping expressions. Next you can see three digits representing a phone number's area code followed by the closing parenthesis. You use a \s to denote a whitespace character. The remainder of the regular expression contains the remaining digits of the phone number.

In addition to the single character classes, you can also use ranged and set character classes. They give you fine-grain control on exactly the type of characters the regular expression should match. For instance, if you want to match any character as long as it is a vowel, use the following expression:

 
 [aeiou] 

This line means that a character should match one of the literal characters within that set of characters. An even more specialized form of single character classes are Unicode character categories. Unicode categories are similar to some of the character-attribute inspection methods shown earlier in this chapter. For instance, you can use Unicode categories to match on uppercase or lowercase characters. Other categories include punctuation characters, currency symbols, and math symbols, to name a few. You can easily find the full list of Unicode categories in MSDN under the topic "Unicode Categories Enumeration."

You can optimize the phone-number expression, although it's completely valid, by using quantifiers . Quantifiers specify additional information about the character, character class, or expression to which it applies. Some quantifiers include wildcards such as * , which means 0 or more occurrences, and ? , which means only 0 or 1 occurrences of a pattern. You can also use braces containing an integer to specify how many characters within a given character class to match. Using this quantifier in the phone-number expression, you can specify that the phone number should contain three digits for the area code followed by three digits and four digits separated by a dash:

 
 \(\d{3}\)\s\d{3}-\d{4} 

Because the regular expression itself isn't that complicated, you can still see that using quantifiers can simplify regular-expression creation. In addition to character classes and quantifiers, you can also use positional information within a regular expression. For instance, you can specify that given an input string, the regular expression should operate at the beginning of the string. You express it using the ^ character. Likewise, you can also denote the end of a string using the $ symbol. Take note that this doesn't mean start at the end of the string and attempt to make a match because that obviously seems counterintuitive; no characters exist at the end of the string. Rather, by placing the $ character following the rest of the regular expression, it means to match the string with the regular expression as long as the match occurs at the end of the string. For instance, if you want to match a sentence in which a phone number is the last portion of the sentence , you could use the following:

 
 \(\d{3}\)\s\d{3}-\d{4}$ My phone number is (555) 555-5555 = Match (555) 555-5555 is my phone number = Not a match 
 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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