Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the function of matching and replacing regular expressions

Shulou Source: shulou.com Published: 2022-06-03 11:02:15 11月15日 Update

This article mainly introduces the regular expression how to achieve matching and replacement function, the article is very detailed, has a certain reference value, interested friends must read!

Regular expression, also known as regular expression. (English: RegularExpression, often abbreviated as regex, regexp or RE in code), a concept in computer science. Regular expressions are often used to retrieve and replace text that conforms to a certain pattern (rule).

Regular expressions match and replace

Input: publicExtension:IExt

Purpose: to match the middle classname and replace it

Expression:

Code:

PrivatestringReplace (stringinput)

{

ReturnRegex.Replace (input,@ "", newMatchEvaluator (RefineCodeTag), RegexOptions.Singleline)

}

StringRefineCodeTag (Matchm)

{

Stringx=m.ToString ()

X=Regex.Replace (x, ",")

Returnx.Trim () + ","

}

Screenshot:

two

Regular expression option RegexOptions:

ExplicitCapture

N

Only groups that are named or numbered are captured

IgnoreCase I is not case sensitive

IgnorePatternWhitespace x eliminates non-escaped whitespace in mode and enables comments marked by #.

MultiLine m

Multiline mode, which works by modifying the meaning of ^ and $

SingleLine s

Single-line mode, corresponding to MultiLine

Other features replaced by regular expressions:

$number replaces the matching number group with a replacement expression

This code returns "010120305"

That is to say, each matching result of group 1 is replaced by the expression "0 $1", and the "$1" in "0 $1" is replaced by the matching result of group 1.

PublicstaticvoidMain ()

{

Strings= "11235"

S=Regex.Replace (s=Regex.Replace @ "(\ d +) (? # this is a comment)," 0 $1 ", RegexOptions.Compiled | RegexOptions.IgnoreCase)

Console.WriteLine (s)

Console.ReadLine ()

These are all the contents of the article "how to match and replace regular expressions". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Expressions regularities patterns functions code content comments articles results rules values interests principles also known as meaning size boys buddies Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MySQL Huawei Shulou Tech Info Xiaomi