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 use pattern string in C #

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

Share

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

This article mainly introduces how to use the pattern string in C#, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.

Using regular expressions to achieve string search, using .NET general expression engine in C #

Through the development of a sample, we will perform and display some search results to illustrate some of the features of general expressions and how to use the .NET general expression engine in C#. Note that the symbol @ should be preceded by a string.

String Text=@ "I can not find my position in Beijing"

This text is called an input string, and in order to illustrate the general expression .NET class, this article first conducts a plain text search without any escape sequence or general expression commands. Suppose you want to find all the strings ion, and call this search string a pattern. Using the general expression and the variable Text declared above, write the following code:

String Pattern = "ion"; MatchCollection Matches = Regex.Matches (Text,Pattern,RegexOptions); foreach (Match NextMatch in Matches) {Console.WriteLine (NextMatch.Index);}

In this code, you use the static method Match () of the Regex class in the System.Text.RegularExpressions namespace. The arguments to this method are some input text, a pattern, and an optional set of flags in each sentence of the RegexOptions. Matches () returns MatchCollection, and each match is represented by a Match object. In the above code, you just iterate through the collection, using the Index property of the Match class, to return the index where the match is in the input text. Run this code and you will get a match.

The functionality of a general collection mainly depends on the C # pattern string. The reason is that the C # mode string contains more than just plain text. As mentioned earlier. It also includes metacharacters and escape sequences, which are special characters that give commands, and the escape sequence works in the same way as the escape sequence of C #. They are characters that begin with a backslash\ and have a special meaning. For example, if you want to find a word that starts with n, you can use the escape sequence\ b, which represents the boundary of a word (the boundary of the word begins with an alphanumeric character, or is followed by a blank character or punctuation). The following code is written:

String Pattern = @ "\ bn"; MatchCollection Matches = Regex.Matches (Text,Pattern,RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture)

To pass\ b to the .NET general expression engine at run time, the backslash\ should not be interpreted by the C# compiler as an escape sequence. If you want to find words ending in the sequence ion, you can use the following code:

String Pattern = @ "ion\ b"

If you want to find all the words that start with the letter n and end with the sequence ion, you need a pattern that starts with\ bn and ends with ion\ b. What about the middle content? You need to tell the computer that the content between n and ion can be characters of any length, as long as the characters are not white space, and the correct pattern is as follows:

String Pattern = @ "\ bn\ S*ion\ b"; Thank you for reading this article carefully. I hope the article "how to use pattern string in C#" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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