The following describes the regular expressions in C #. The regular expressions in C # are contained in a namespace of the. NET base record. The namespace is System. Text. RegularExpressions. The namespace contains eight classes, one enumeration, and one delegate. They are:
Capture: contains a matching result;
CaptureCollection: the sequence of Capture;
Group: the result of a Group record, inherited by Capture;
GroupCollection: a collection of capture groups.
Match: the matching result of an expression, inherited by the Group;
MatchCollection: a sequence of Match;
MatchEvaluator: The delegate used to perform the replacement operation;
Regex: An Example of the compiled expression.
RegexCompilationInfo: provides information that the compiler uses to compile a regular expression into an independent assembly.
RegexOptions provides the enumerated values used to set regular expressions.
The Regex class also contains some static methods:
Escape: Escape the Escape characters in the regex string;
IsMatch: If the expression matches a string, this method returns a Boolean value;
Match: returns the instance of the Match;
Matches: returns a series of Match methods;
Replace: Replace the matching expression with the replacement string;
Split: returns a series of strings determined by expressions;
Unescape: do not escape characters in strings.
First, we start from using simple expressions of the Regex and Match classes:
Code
Regex emailregex = new Regex ("(? <User> [^ @] + )@(? <Host>. + )");
String s = "johndoe@tempuri.org ";
Match m = emailregex. Match (s );
If (m. Success)
{
System. Console. WriteLine ("User:" + m. Groups ["user"]. Value );
System. Console. WriteLine ("Host:" + m. Groups ["host"]. Value );
}
Else
{
System. Console. WriteLine (s + "is not a valid email address ");
}
The IsMatch method indicates whether the regular expression specified by the Regex constructor finds a match in the input string. This is one of the most common methods when we use a C # regular expression. The following example illustrates how to use the IsMatch method:
Code
Regex emailregex = new Regex ("(? <User> [^ @] + )@(? <Host>. + )");
If (emailregex. IsMatch ("ddd *. com "))
{
System. Console. WriteLine ("Matched successfully ");
}
Else
{
System. Console. WriteLine ("Matched failed ");
}
System. Console. ReadLine ();
The Split method splits the input string into a substring array at the position defined by the regular expression match. For example:
Code
Regex r = new Regex ("-"); // Split on hyphens.
String [] s = r. Split ("first-second-third ");
For (int I = 0; I <s. Length; I ++)
{
Response. Write (s [I] + "<br> ");
}
The execution result is:
First
Second
Third
The Split method looks the same as the Split method of String, but the Split method of string splits the String in a separator determined by a regular expression rather than a group of characters.
The Match method searches for matching items of the regular expression in the input string, and the Match method of the Regex class returns the Match object. The Match class indicates the matching operation result of the regular expression. The following example demonstrates the use of the Match Method and uses the Group attribute of the Match object to return the Group Object:
Code
String text = @ "public string testMatchObj string s string match ";
String pat = @ "(\ w +) \ s + (string )";
// Compile the regular expression.
Regex r = new Regex (pat, RegexOptions. IgnoreCase );
// Match the regular expression pattern against a text string.
Match m = r. Match (text );
Int matchCount = 0;
While (m. Success)
{
Response. Write ("Match" + (++ matchCount) + "<br> ");
For (int I = 1; I <= 2; I ++)
{
Group g = m. Groups [I];
Response. Write ("Group" + I + "= '" + g + "'" + "<br> ");
CaptureCollection cc = g. Captures;
For (int j = 0; j <cc. Count; j ++)
{
Capture c = cc [j];
Response. Write ("Capture" + j + "= '" + c + "', Position =" + c. Index + "<br> ");
}
}
M = m. NextMatch ();
}
The running result of this example is:
Matebook
Group1 = 'public'
Capture0 = 'public', Position = 0
Group2 = 'string'
Capture0 = 'string', Position = 7
Match2
Group1 = 'testmatchobj'
Capture0 = 'testmatchobj ', Position = 14
Group2 = 'string'
Capture0 = 'string', Position = 27
Match3
Group1 = 'S'
Capture0 ='s, Position = 34
Group2 = 'string'
Capture0 = 'string', Position = 36