C Regular Expression Match and Replace String Feature Sample

  • 2021-12-19 06:35:52
  • OfStack

This article illustrates the C # regular expression matching and replacing string function. Share it for your reference, as follows:

Case 1:\ w+= > [A-Za-z1-9_],\s+= > Any white space 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 running result of this case is:

Match1
Group1='public'
Capture0='public', Position=0
Group2='string'
Capture0='string', Position=7
Match2
Group1='testMatchObj'
Capture0='testMatchObj', Position=14
Group2='string'
Capture0='string', Position=27
Match3
Group1='s'
Capture0='s', Position=34
Group2='string'
Capture0='string', Position=36

Example 2:


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 "[]". Yes, replace them with " < > "

Regular expressions in C # are contained under a namespace of the. NET base class library, which is System.Text.RegularExpressions . The namespace includes 8 classes, 1 enumeration and 1 delegate. They are:

Capture: Contains the result of one match;
CaptureCollection: The sequence of Capture;
Group: The result of one group record, inherited from Capture;
GroupCollection: Represents a collection of capture groups
Match: The matching result of the expression of degree 1, which is inherited from Group;
MatchCollection: One sequence of Match;
MatchEvaluator: Delegate used when performing a replace operation;
Regex: An instance of the compiled expression.
RegexCompilationInfo: Provides information that the compiler uses to compile regular expressions into stand-alone assemblies
RegexOptions provides enumerated values for setting regular expressions

The Regex class also contains one static method:

Escape: Escape the escape character in regex in the string;
IsMatch: This method returns 1 Boolean value if the expression matches in the string;
Match: Returns an instance of Match;
Matches: A method that returns Match of Series 1;
Replace: Replace a matching expression with a replacement string;
Split: Returns 1 series of strings determined by expressions;
Unescape: Escaped characters in strings are not escaped.

PS: Here are two very convenient regular expression tools for your reference:

JavaScript Regular Expression Online Test Tool:
http://tools.ofstack.com/regex/javascript

Regular expression online generation tool:
http://tools.ofstack.com/regex/create_reg

For more readers interested in C # related content, please check the topics on this site: "C # Regular Expression Usage Summary", "C # Coding Operation Skills Summary", "XML File Operation Skills Summary in C #", "C # Common Control Usage Tutorial", "C # Data Structure and Algorithm Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "C # Programming Thread Use Skills Summary"

I hope this article is helpful to everyone's C # programming.


Related articles: