C # Regular Expression notes,
C # Regular Expression notes
It may take several days.
See a good regular expression. The regular expression tutorials include the string tutorial csf-character string and the regular expression reference manual.
The namespace required by the regular expression is using System. Text. RegularExpressions.
It contains 8 classes, the most used is Regex;
Regex can be used not only to create regular expressions, but also provides many useful methods.
Create a Regex object
New Regex (string pattern)
New Regex (string pattern, RegexOptions options)
The first parameter is a string. The second parameter has the following regular expression options:
- IgnoreCase // Yes matching case-insensitive by default case-sensitive
- RightToLeft // The right-to-left search string is left-to-right by default.
- None // do not set flag this is the default option, that is, do not set 2nd parameters to indicate case-sensitive from left to right
- MultiLinc // specify the start and end of the ^ and $ matching rows. That is to say, the line feed is used for segmentation, and each row can be matched differently.
- SingleLine // specifies the special character ". "match any character, except for line breaks. special characters by default ". "does not match the line feed. (The line feed parameter does not match any meaning. Why)
IgnoreCase example
String test = "Abcccccc ";
Regex reg = new Regex ("abc ");
Console. WriteLine (reg. IsMatch (test); // false
Regex reg1 = new Regex ("abc", RegexOptions. IgnoreCase); // case insensitive
Console. WriteLine (reg1.IsMatch (test); // true
RightToLeft example
String test = "vvv123 === 456vvv ";
Regex reg = new Regex ("\ d +"); // 123 matches consecutive numbers from left to right
Console. WriteLine (reg. Match (test ));
Regex reg1 = new Regex ("\ d +", RegexOptions. RightToLeft );
Console. WriteLine (reg1.Match (test); // 456 matches consecutive numbers from right to left
MultiLinc example
StringBuilder input = new StringBuilder ();
Input. AppendLine ("A bbbb ");
Input. AppendLine ("C bbbb C ");
String pattern = @ "^ \ w ";
Console. WriteLine (input. ToString ());
MatchCollection matchCol = Regex. Matches (input. ToString (), pattern, RegexOptions. Multiline );
Foreach (Match item in matchCol)
{
Console. WriteLine ("Result: {0}", item. Value );
}
IsMatch ()
It can be used to test the string to see if it matches the regular expression pattern. If a match is found, True is returned. IsMatch has a static method overload.
Regex. IsMatch (string str, string pattern );
String str = "abcbbbbbbbbbb ";
String reg = @ "^ abc ";
Console. WriteLine (Regex. IsMatch (str, reg); // static overload method
Regex pattern = new Regex ("^ abc ");
Console. WriteLine (pattern. IsMatch (str); // method on the generated object
Replace ()
Replace a string with a matching pattern. There is also a static overload method. There are many replace Variants. I only record what I see.
Replace (string input, string pattern, int count, int start) 3rd parameters are replaced in total, and the 4th parameter is replaced by the position of the string.
String str = "123456abc ";
Regex pattern = new Regex (@ "\ d + ");
Console. WriteLine (pattern. Replace (str, "6666 "));
String pattern1 = @ "\ d + ";
Console. WriteLine (Regex. Replace (str, pattern1, "6666 "));
String str1 = "asd, sss, asd, asdf, jjjj, cccc ";
Regex pattern2 = new Regex (@ "\ w + ");
Console. WriteLine (pattern2.Replace (str1, "v5v5", 2 ));
Console. WriteLine (pattern2.Replace (str1, "v5v5", 2, 8 ));
// Console. WriteLine (Regex. Replace (str1, @ "\ w +", "v5v5", 2); If the static method fails, an error is returned.
Match ()
Get matched content (only one MatchCollection can get all matched sets)
Usage of methods on the generated object
Reg. Match (string input, int start, int length)
The first parameter is the starting position of the second brother of the string to be processed. The second parameter is the length to be matched. 2nd 3rd parameters are not required
Static Method Regex. Match (string input, string pattern, RegexOptions options)
3rd parameters are optional
string str = "vchaha vcoo vclielie vbguale vfgg vckk";
Regex pattern = new Regex(@"vc\w*");
Match matchMode = pattern.Match(str);
while (matchMode.Success)
{
Console.WriteLine(matchMode.Value);
matchMode = matchMode.NextMatch();
}
Console.WriteLine("-----------------------------------");
Match matchMode1 = Regex.Match(str, @"vc\w*");
while (matchMode1.Success)
{
Console.WriteLine(matchMode1.Value);
matchMode1 = matchMode1.NextMatch();
}
Methods of the Match Class
- NextMatch returns the next matched match object.
- Result
- Value Returns the matched string.
- Length matching Length
- The starting position of the First Matching content of Index in the string
- Groups returns a group object set.
- Success returns true or false based on whether the matching is successful.
MatchCollection ()
Regex. Matchs returns the MatchCollection class, which contains all the matching sets.
string input = "hahaha 123xiaodi 55nihao 66chifanlema ccc333 ccc";
Regex pattern = new Regex(@"\d+[a-z]+",RegexOptions.IgnoreCase);
MatchCollection matchsMade = pattern.Matches(input);
foreach (Match item in matchsMade)
{
Console.WriteLine(item.Value);
}