string s = "This is a string";
Regex r = new Regex("string");
if (r.IsMatch(s)) {
Console.WriteLine("Matches.");
}
Match m = r.Match(s);
if (m.Success) {
Console.WriteLine("Match found at " + m.Index); //---Match found at 10---
}
What if you have multiple matches in a string? In this case, you can use the Matches()
method of the RegEx
class. This method returns a MatchCollection
object, and you can iteratively loop through it to obtain the index positions of each individual match:
string s = "This is a string and a long string indeed";
Regex r = new Regex("string");
MatchCollection mc = r.Matches(s);
foreach (Match m1 in mc) {
Console.WriteLine("Match found at " + m1.Index);
//---Match found at 10---
//---Match found at 28---
}
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)\d\d[- /.](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)\d\d[- /.](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-\x1F\x7F] |
Control characters |
[\x21-\x7E] |
Visible characters |
[\x20-\x7E] |
Visible characters and spaces |
[!"#$%&'()*+,-./:;<=>?@[\\\]_`{|}~] |
Punctuation characters |
[ \t\r\n\v\f] |
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 = "weimenglee@learn2develop.net";
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.
String manipulations are common operations, so it's important that you have a good understanding of how they work and the various methods and classes that deal with them. This chapter provided a lot of information about how strings are represented in C# and about using regular expressions to perform matching on strings.
One of the new features in the .NET Framework (beginning with version 2.0) is the support of generics in Microsoft Intermediate Language (MSIL). Generics use type parameters, which allow you to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code. Generics enable developers to define type- safe data structures, without binding to specific fixed data types at design time.
Generics are a feature of the IL and not specific to C# alone, so languages such as C# and VB.NET can take advantage of them.
This chapter discusses the basics of generics and how you can use them to enhance efficiency and type safety in your applications. Specifically, you will learn:
□ Advantages of using generics
□ How to specify constraints in a generic type
□ Generic interfaces, structs, methods, operators, and delegates
□ The various classes in the .NET Framework class library that support generics
Let's look at an example to see how generics work. Suppose that you need to implement your own custom stack class. A stack is a last-in, first-out (LIFO) data structure that enables you to push items into and pop items out of the stack. One possible implementation is:
Читать дальше