Notes on C #. net regular expression Learning

Source: Internet
Author: User

1. system. Text. regularexpression namespace
1, RegEx class It can be used to create regular expressions and provides many methods.
For example, RegEx. Replace (string input, string pattern, string replacement );
------- Regexoption Enumeration
Ignorecase Case Insensitive. Case Sensitive by default
Righttoleft Search for the input string from right to left.
None Do not set a flag.
Miltiline Specify ^ and $ to match the beginning and end of the line, and the start and end of the string.
Singleline Specifies that the special character "." matches any character. Except for line breaks.
Example: regexoptions. ignorecase
RegEx. ismatch (mystring, "ywsm", regexoptions. ignorecase | regexoptions. righttoleft ):
------- (Two main) Class Constructor
RegEx (string pattern );
RegEx (string pattern, regexoption options );
Example: Match ywsm:
Static void main (string [] ARGs)
{RegEx myregex = new RegEx ("ywsm ");
System. Console. writeline (myregex. ismatch ("the first three letters of" + "the alphabet are ywsm "));}
Output: True. Case Sensitive and available if needed
RegEx myregex = new RegEx ("ywsm", regexoption. ignorecase );
------- Ismatch () method
This method can test the string to see if it matches the regular expression pattern. If a match is found, true is returned; otherwise, false is returned. Ismatch () has a static overload method. When using it, you do not need to explicitly create a RegEx object.
Reload mode:
Public bool RegEx. ismatch (string input );
Public bool RegEx. ismatch (string input, int startat );
Public static bool RegEx. ismatch (string input, string pattern );
Public static bool RegEx. ismatch (string input, string pattern, regexoption options );
Input: Specifies a string containing the text to be retrieved.
Sartat: Specifies the start character position of the search.
Pttern: Specifies the style to be matched.
Options: the option that matches the behavior.
Example: String inputstring = "welcome to the ywicc, ywsmxy! ";
If (RegEx. ismatch (inputstring, "ywicc", regexoptions. ignorecase ))
Console. writeline ("match found ");
Else
Console. writeline ("no match found ");
------ Replace () method
Replace a matching pattern with the specified string.
--- Basic methods:
Public static string RegEx. Replace (string input, string pattern, string replacement );
Public static string RegEx. Replace (string input, string pattern, string replacement, regexoption options );
For example, replace all "BBB" instances with "AAA" Code :
String inputstring = "welcome to the AAA! ";
Inputstring = RegEx. Replace (inputstring, "BBB", "AAA ");
Console. writeline (inputstring );
---- For non-static methods, you can specify the maximum replacement times and the start Subscript:
Public String Replace (string input, string replacement );
Public String Replace (string input, string replacement, int count );
Public String Replace (string input, string replacement, int count, int startat );
For example, replace 456 after 123 with xxx. The Code is as follows:
String inputstring = "123,456,123,123,123,789,333 ";
RegEx Regexp = new RegEx ("123 ");
Inputstring = Regexp. Replace (inputstring, "XXX", 2, 4)
Console. writeline (inputstring );
Output: 123,456, XXX, XXX, 123,789,333
------- Split () method
Splits the string at the specified position. Returns an array of strings.
Using system;
Using system. text;
Using system. Text. regularexpressions;
Using system. Windows. forms;
Class mysplit
{
Static void main (string [] ARGs)
{
String inputstring = "123,456,789, ads ";
String [] splitresults;
Splitresults = RegEx. Split (inputstring ,",");
Stringbuilder resultsstring = new stringbuilder (32 );
Foreach (string stringelement in splitresults)
{
Resultsstring. append (stringelement + "\ n ");
}
MessageBox. Show (resultsstring. tostring ());
}
}

123 <= Result
456
789
Ads

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.