Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of matching and replacing regular expressions

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report