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 understand C # regular expressions

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to understand C# regular expressions". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn how to understand C# regular expressions.

So far, many programming languages and tools include support for regular expressions, and .NET is no exception. The .NET basic class library contains a namespace and a series of classes that take full advantage of regular expressions.

Knowledge of regular expressions is probably the most annoying thing for many programmers. If you don't already have knowledge of regular expressions, it is recommended to start with the basics of regular expressions. See regular expression syntax before.

Let's take a look at C # regular expressions, which are contained in a namespace of the .NET basic mine library, which is System.Text.RegularExpressions. The namespace consists of 8 classes, 1 enumeration, and 1 delegate. They are:

◆ Capture: contains the result of a match

◆ CaptureCollection: the sequence of Capture

◆ Group: the result of a group record, inherited by Capture

◆ GroupCollection: represents a collection of capture groups

◆ Match: the matching result of an expression, inherited from Group

◆ MatchCollection: a 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 some static methods:

◆ Escape: escapes escape characters in regex in a string

◆ IsMatch: if the expression matches in a string, this method returns a Boolean value

◆ Match: returns the instance of Match

◆ Matches: a method that returns a series of Match

◆ Replace: replace matching expressions with replacement strings

◆ Split: returns a series of strings determined by expressions

◆ Unescape: does not escape escaped characters in a string.

Their uses are described below:

Let's start with a simple example of matching. Let's start by using simple expressions of Regex and Match classes. Match m = Regex.Match ("abracadabra", "(a | b | r) +"); we now have an instance of the Match class that can be used for testing, for example: if (m.Success) {}, if you want to use a matching string, you can convert it to a string: MesaageBox.Show ("Match=" + m.ToString ()); this example produces the following output: Match=abra. This is the matching string.

The Regex class represents a read-only regular expression class. It also contains a variety of static methods (described one by one in the following examples) that allow other regular expression classes to be used without explicitly creating instances of other classes.

The following code example creates an instance of the Regex class and defines a simple regular expression when initializing the object. Declare a Regex object variable: Regex objAlphaPatt;, then creates an instance of the Regex object and defines its rule: objAlphaPatt=new Regex ("[^ a-zA-Z]")

The IsMatch method indicates whether the regular expression specified in the Regex constructor finds a match in the input string. This is one of the most common methods we use when using C# regular expressions. The following example illustrates the use of the IsMatch method:

If (! objAlphaPatt.IsMatch ("testisMatchMethod")) lblMsg.Text = "match succeeded"; else lblMsg.Text = "match failed"; the result of this code execution is "match success" if (! ObjAlphaPatt.IsMatch ("testisMatchMethod7654298") lblMsg.Text = "match succeeded"; else lblMsg.Text = "match not successful"

The Split method splits the input string into an array of substrings at the position defined by the regular expression match. For example:

Regex r = new Regex ("-") / / Split on hyphens. String [] s = r.Split ("first-second-third"); for (int item0)

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