C # Regular Expression notes
The namespace required for the regular is the using System.Text.RegularExpressions
It contains 8 classes, with the most use of a regex;
Regex can be used not only to create regular expressions, but also to provide a number of 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 of the regular configuration option has some of the following options
- IgnoreCase//is a match ignoring case is case-sensitive by default
- RightToLeft//Right-to-left lookup string default is left to right
- None//Do not set flag This is the default option, that is, do not set the 2nd parameter to indicate case sensitivity from left to right
- Multilinc//Specifies that ^ and $ can match the beginning and end of a line, which means that a newline split is used, and each row can be matched differently
- Singleline//Specify special characters "." Matches any one character, except for line breaks. By default, the special character "." Does not match the line break. ( what does not mean to match the parameter of the line is not understood )
Examples of ignorecase
stringTest ="ABCCCCCC";
Regex reg =NewRegex ("ABC");
Console.WriteLine (Reg. IsMatch (test));//false
Regex REG1 =NewRegex ("ABC", regexoptions.ignorecase);// Case insensitive
Console.WriteLine (REG1. IsMatch (test));//true
Examples of RightToLeft
string " VVV123===456VVV ";
New Regex ("\\d+"); // 123 matching consecutive numbers from left to right
Console.WriteLine (Reg. Match (test));
New Regex ("\\d+", Regexoptions.righttoleft);
Console.WriteLine (REG1. Match (test)); // 456 matching consecutive numbers from right to left
Examples of Multilinc
StringBuilder input =NewStringBuilder ();
Input. Appendline ("a bbbb a");
Input. Appendline ("C BBBB C");
stringPattern =@"^\w";
Console.WriteLine (input. ToString ());
MatchCollection matchcol = regex.matches (input. ToString (), pattern, regexoptions.multiline);
foreach(Match IteminchMatchcol)
{
Console.WriteLine ("result: {0}", item. Value);
}
IsMatch ()
Can be used to test a string to see if he matches the pattern of a regular expression. If a match is found, return True.ismatch has a static method overload
Regex.IsMatch (string str,string pattern);
string " abcbbbbbbbb ";
string @" ^ABC ";
Console.WriteLine (Regex.IsMatch (Str,reg)); // Static Overloaded Methods
New Regex ("^abc");
// methods on the Build object
Replace ()
Replace string a matching pattern, there is also a static overloaded method, the Replace Variant method is a lot, I only record what I see
Replace (string input, String Pattern,int count,int start) The 3rd parameter is a total substitution of a few, and the 4 parameter is where the string begins to replace
stringstr ="123456ABC";
Regex pattern =NewRegex (@"\d+");
Console.WriteLine (Pattern. Replace (str,"6666"));
stringPATTERN1 =@"\d+";
Console.WriteLine (Regex.Replace (STR,PATTERN1,"6666"));
stringSTR1 ="ASD,SSS,ASD,ASDF,JJJJ,CCCC";
Regex pattern2 =NewRegex (@"\w+");
Console.WriteLine (pattern2. Replace (STR1,"v5v5",2));
Console.WriteLine (pattern2. Replace (STR1,"v5v5",2,8));
//Console.WriteLine (Regex.Replace (STR1, @ "\w+", "v5v5", 2)); static method It doesn't sound like a bug. Haha
Match ()
Get the matching content (just once the matchcollection can get all the matching collections)
The use of methods on the generated objects
Reg. Match (string input,int start,int length)
The first parameter is the string to be processed the second brother argument starts at the position 3rd parameter is the length that needs to be matched. 2nd 3rd parameter can not be required
static method Regex.match (string input, string pattern,regexoptions options)
The 3rd parameter can be used instead of
stringstr ="vchaha vcoo vclielie vbguale vfgg Vckk";
Regex pattern =NewRegex (@"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 ();
}
Some methods of the match class
- NextMatch returns the next successfully matched match object
- Result
- Value returns a matching string
- Length-Matched Lengths
- Index the start position of the first match in the string
- Groups returns a collection of grouped objects
- Success returns ture or false based on whether the match is successful
matchcollection ()
Regex.matchs will return the MatchCollection class, which contains all the match collections
string " hahaha 123xiaodi 55nihao 66chifanlema ccc333 CCC ";
New Regex (@ "\d+[a-z]+", regexoptions.ignorecase);
MatchCollection Matchsmade = pattern. Matches (input);
foreach in Matchsmade)
{
Console.WriteLine (item. Value);
}
C # Regular Expression notes