This article mainly introduced the C # Regular expression matching and substitution string function, combined with the concrete instance form Analysis C # string regular substitution related class, the method uses the skill and the related attention matter, needs the friend can refer to the next
This example describes the C # regular expression matching and substitution string functionality. Share to everyone for your reference, as follows:
case One:\w+=>[a-za-z1-9_],\s+=> any whitespace character, () = = Capture
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 result of this example operation is:
match1group1= ' public ' capture0= ' public ', position=0group2= ' string ' capture0= ' string ', position=7match2group1= ' Testmatchobj ' capture0= ' testmatchobj ', position=14group2= ' string ' capture0= ' string ', position=27match3group1= ' S ' capture0= ' s ', position=34group2= ' string '
Capture0= ' string ', position=36
Case TWO:
string x = This.txt.Text; RegexOptions ops = Regexoptions.multiline; Regex r = new Regex (@ "\[(. +?) \] ", OPS); //\[(.+?) \/\] @"\[(.+)\](.*?) \[\/\1\] "//response.write (R.ismatch (x). ToString () +datetime.now.tostring ()); if (R.ismatch (x)) { x = r.replace (x, "<$1>"); Response.Write (x.tostring () + DateTime.Now.ToString ()); Console.WriteLine ("var x:" + x);//output: Live for nothing}else{ Response.Write ("false" + DateTime.Now.ToString ());}
This is to replace the "[]" pair, change them to "<>"
Regular expressions in C # are included in the. NET base Class library under a namespace, this namespace is System.Text.RegularExpressions
. The namespace consists of 8 classes, 1 enumerations, and 1 delegates. They were:
Capture: Contains the result of a match;
capturecollection: the sequence of capture;
Group: The result of a set of records, which is inherited from capture;
GroupCollection: Represents a collection of capturing groups
match: The matching result of an expression is inherited by group;
matchcollection: a sequence of match;
MatchEvaluator: The delegate used when performing the replace operation;
Regex: An instance of the compiled expression.
RegexCompilationInfo: Provides information that the compiler uses to compile a regular expression into a stand-alone assembly
RegexOptions provides an enumeration value to set the regular expression
The Regex class also contains some static methods:
escape : Escapes the escape character in a regex in a string,
ismatch : If the expression matches in a string, the method returns a Boolean value;
match : Returns an instance of match;
matches : Returns a series of match methods;
replace : Replace the matching expression with a replacement string;
Span style= "COLOR: #0000ff" >split : Returns a sequence of strings determined by an expression;
unescape : Escape characters in a string are not escaped.