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 |
| 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 |
Вы читаете C# 2008 Programmer's Reference