Книга: C# 2008 Programmer

More Complex Pattern Matching

More Complex Pattern Matching

You can specify more complex searches using regular expressions operators. For example, to know if a string contains either the word "Mr" or "Mrs", you can use the operator |, like this:

string gender = "Mr Wei-Meng Lee";
Regex r = new Regex("Mr|Mrs");
if (r.IsMatch(gender)) {
 Console.WriteLine("Matches.");
}

The following table describes regular expression operators commonly used in search patterns.

Operator Description
. Match any one character
[ ] Match any one character listed between the brackets
[^ ] Match any one character not listed between the brackets
? Match any character one time, if it exists
* Match declared element multiple times, if it exists
+ Match declared element one or more times
{n} Match declared element exactly n times
{n,} Match declared element at least n times
{n,N} Match declared element at least n times, but not more than N times
^ Match at the beginning of a line
$ Match at the end of a line
< Match at the beginning of a word
> Match at the end of a word
b Match at the beginning or end of a word
B Match in the middle of a word
d Shorthand for digits (0-9)
w Shorthand for word characters (letters and digits)
s Shorthand for whitespace

Another common search pattern is verifying a string containing a date. For example, if a string contains a date in the format "yyyy/mm/dd", you would specify the search pattern as follows: "(19|20)dd[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])". This pattern will match dates ranging from 1900-01-01 to 2099-12-31.

string date = "2007/03/10";
Regex r = new Regex(@"(19|20)dd[- /.](0[1-9]|1[012])[- /.] (0[1-9]|[12][0-9]|3[01])");
if (r.IsMatch(date)) {
 Console.WriteLine("Matches.");
}

You can use the following date separators with the pattern specified above:

string date = "2007/03/10"
string date = "2007-03-10"
string date = "2007 03 10"
string date = "2007.03.10"

Some commonly used search patterns are described in the following table.

Pattern Description
[0-9] Digits
[A-Fa-f0-9] Hexadecimal digits
[A-Za-z0-9] Alphanumeric characters
[A-Za-z] Alphabetic characters
[a-z] Lowercase letters
[A-Z] Uppercase letters
[ t] Space and tab
[x00-x1Fx7F] Control characters
[x21-x7E] Visible characters
[x20-x7E] Visible characters and spaces
[!"#$%&'()*+,-./:;<=>?@[]_`{|}~] Punctuation characters
[ trnvf] Whitespace characters
w+([-+.']w+)*@w+([-.]w+)*.w+([-,]w+)* Email address
http(s)?://([w-]+.)+[w-]+(/[w- ./?%&=]*)? Internet URL
(((d{3}) ?)|(d{3}-))?d{3}-d{4} U.S. phone number
d{3}-d{2}-d{4} U.S. Social Security number
d{5}(-d{4})? U.S. ZIP code

To verify that an email address is correctly formatted, you can use the following statements with the specified regular expression:

string email = "[email protected]";
Regex r = new Regex(@"^[w-.]+@([w-]+.)+[w-]{2,4}$");
if (r.IsMatch(email))
 Console.WriteLine("Email address is correct.");
else
 Console.WriteLine("Email address is incorrect.");

There are many different regular expressions that you can use to validate an email address. However, there is no perfect regular expression to validate all email addresses. For more information on validating email addresses using regular expressions, check out the following web sites: http://regular-expressions.info/email.html and http://fightingforalostcause.net/misc/2006/compare-email-regex.php.

Оглавление книги

Оглавление статьи/книги

Генерация: 1.434. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз