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 the positioning characters of C # regular expressions

2025-01-17 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 positioning characters of C# regular expressions". In the daily operation, I believe many people have doubts about how to use the positioning characters of C# regular expressions. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about how to use the positioning characters of C# regular expressions. Next, please follow the editor to study!

1. The popularity of the two ows tables under the symbol "@". Although Dangchen is not a "member" of the C# regular expression, it is often coupled with the C# regular expression. " @ "means that the string that follows it is a" verbatim string ", which is not easy to understand. For example, the following two declarations are equivalent: string x =" D:\ My Huang\\ My Doc "; string y = @" D:\ My Huang\ My Doc " In fact, if you declare as follows, C# will report an error because "\" is used to escape in C#, such as "\ n" line break: string x = "D:\ My Huang\ My Doc"

two。 Basic syntax characters.

The complement of the number\ D\ d of\ d 0 # 9 (all characters are the complete set, the same below, that is, all non-numeric characters\ w word characters, refers to uppercase and lowercase letters, 0-9 numbers, underscore\ W\ w complement\ s white space characters, including line feed\ n, carriage return\ r, tab\ t, vertical tab\ v, feed\ f\ S\ s complement. Any character except the newline character\ n […] Match all the characters listed in [] [^...] Some simple examples are provided below to match characters not listed in []:

String I = "\ n"; string m = "3"; Regex r = new Regex (@ "\ D"); / / same as Regex r = new Regex ("\\ D"); / / r.IsMatch (I) result: true / / r.IsMatch (m) result: false string i = "%"; string m = "3"; Regex r = new Regex ("[a-z0-9]") / / match lowercase letters or numeric characters / / r.IsMatch (I) result: false / / r.IsMatch (m) result: true

3. The positioning character "positioning character" represents a virtual character, which represents a position, and you can also intuitively think that "positioning character" represents the small gap between a character and a character.

^ indicates that the character that follows must be at the beginning of the string $means that the character before it must be at the end of the string\ b matches the boundary of a word\ B matches the boundary of a non-word in addition, it also includes: the character before\ A must be at the beginning of the character, the character before\ z must be at the end of the string, and the character before\ Z must be at the end of the string. Or the following provides some simple examples before the newline character:

String I = "Live for nothing,die for something"

Regex R1 = new Regex ("^ Live for nothing,die for something$")

/ / r1.IsMatch (I) true

Regex R2 = new Regex ("^ Live for nothing,die for some$")

/ / r2.IsMatch (I) false

Regex R3 = new Regex ("^ Live for nothing,die for some")

/ / r3.IsMatch (I) true

String I = @ "Live for nothing

Die for something "; / / multiple lines

Regex R1 = new Regex ("^ Live for nothing,die for something$")

Console.WriteLine ("R1 match count:" + r1.Matches (I) .count); / / 0

Regex R2 = new Regex ("^ Live for nothing,die for something$"

RegexOptions.Multiline)

Console.WriteLine ("R2 match count:" + r2.Matches (I) .count); / / 0

Regex R3 = new Regex ("^ Live for nothing,\ r\ ndie for something$")

Console.WriteLine ("R3 match count:" + r3.Matches (I) .count); / / 1

Regex R4 = new Regex ("^ Live for nothing,$")

Console.WriteLine ("R4 match count:" + r4.Matches (I) .count); / / 0

Regex R5 = new Regex ("^ Live for nothing,$", RegexOptions.Multiline)

Console.WriteLine ("R5 match count:" + r5.Matches (I) .count); / / 0

Regex R6 = new Regex ("^ Live for nothing,\ r\ n$")

Console.WriteLine ("R6 match count:" + r6.Matches (I) .count); / / 0

Regex R7 = new Regex ("^ Live for nothing,\ r\ n$", RegexOptions.Multiline)

Console.WriteLine ("R7 match count:" + r7.Matches (I) .count); / / 0

Regex R8 = new Regex ("^ Live for nothing,\ r $")

Console.WriteLine ("R8 match count:" + r8.Matches (I) .count); / / 0

Regex R9 = new Regex ("^ Live for nothing,\ r $", RegexOptions.Multiline)

Console.WriteLine ("R9 match count:" + r9.Matches (I) .count); / / 1

Regex R10 = new Regex ("^ die for something$")

Console.WriteLine ("R10 match count:" + r10.Matches (I) .count); / / 0

Regex R11 = new Regex ("^ die for something$", RegexOptions.Multiline)

Console.WriteLine ("R11 match count:" + r11.Matches (I) .count); / / 1

Regex R12 = new Regex ("^")

Console.WriteLine ("R12 match count:" + r12.Matches (I) .count); / / 1

Regex R13 = new Regex ("$")

Console.WriteLine ("R13 match count:" + r13.Matches (I) .count); / / 1

Regex R14 = new Regex ("^", RegexOptions.Multiline)

Console.WriteLine ("R14 match count:" + r14.Matches (I) .count); / / 2

Regex R15 = new Regex ("$", RegexOptions.Multiline)

Console.WriteLine ("R15 match count:" + r15.Matches (I) .count); / / 2

Regex R16 = new Regex ("^ Live for nothing,\ r $\ n ^ die for something$"

RegexOptions.Multiline)

Console.WriteLine ("R16 match count:" + r16.Matches (I) .count); / / 1

/ / for a multiline string, ^ and $will match multiple times after the Multiline option is set.

String I = "Live for nothing,die for something"

String m = "Live for nothing,die for some thing"

Regex R1 = new Regex (@ "\ bthing\ b")

Console.WriteLine ("R1 match count:" + r1.Matches (I) .count); / / 0

Regex R2 = new Regex (@ "thing\ b")

Console.WriteLine ("R2 match count:" + r2.Matches (I) .count); / / 2

Regex R3 = new Regex (@ "\ bthing\ b")

Console.WriteLine ("R3 match count:" + r3.Matches (m) .count); / / 1

Regex R4 = new Regex (@ "\ bfor something\ b")

Console.WriteLine ("R4 match count:" + r4.Matches (I) .count); / / 1

/ /\ b is usually used to constrain a complete word

At this point, the study of "how to use the positioning characters of C# regular expressions" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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