C # Regular Expression notes

Source: Internet
Author: User

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);
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.