In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "what are the knowledge points of C# regular expressions?", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "what are the knowledge points of C# regular expression" article.
I. brief introduction
A regular expression is a pattern that matches input text, and the. Net framework provides a regular expression engine that allows such matching, and the pattern consists of one or more characters, operators, and structures The common characters, operators, and structures used to define various categories of regular expressions are listed below.
Character escape
The backslash character (\) in a regular expression indicates that the character that follows it is a special character or that it should be interpreted literally.
Escape character description pattern match\ a matches the alarm (bell) character\ u0007. The "\ u0007"\ b in "Warning!" +'\ u0007' matches the backspace key\ u0008 in the character class. [\ b] {3,} "\ b\ b" in "\ b\ b" matches the tab\ u0009. "Name\ t" and "Addr\ t"\ r in "Name\ tAddr\ t" match the carriage return character\ u000D. (\ r is not equivalent to the newline character\ n. )\ r\ n (\ w +) "\ r\ nHello\ nWorld." The "\ r\ nHello"\ v in matches the vertical tab character\ u000B. "\ v\ v\ v" in [\ v] {2,} "\ v\ v\ v" matches the page feed character\ u000C. The "\ f\ f\ f" in [\ f] {2,} "\ f\ f\ f" matches the newline character\ u000A. \ r\ n (\ w+) "\ r\ nHello\ nWorld." The "\ r\ nHello"\ e in matches the escape character\ u001B. The "\ x001B"\ nnn in\ e "\ x001B" specifies a character in octal representation (nnn consists of two to three digits). The "a b" and "c d"\ x nn in\ w\ 040\ w "a bc d" specify characters using hexadecimal representations (nn happens to be made up of two digits). The "a b" and "c d"\ c X\ c x in\ w\ x20\ w "a bc d" match the ASCII control characters specified by X or x, where X or x is the letter of the control character. The "\ x0003" (Ctrl-C)\ u nnnn in\ cC "\ x0003" matches a Unicode character (a four-digit number represented by nnnn) in hexadecimal representation. The words "a b" and "c d"\ in\ w\ u0020\ w "a bc d" match with an unrecognized escape character. \ d + [\ +-x\ *]\ d + [\ +-x\ *\ d + "(2) * 3" 9 "character classes in" 2 "and" 3 "9
The character class matches any one of a set of characters.
The character class describes the pattern match [character_group] matches any single character in the character_group. By default, matches are case sensitive. [mn] "m" in "mat", "m" and "n" [^ character_group] in "moon" are not: match any single character that is not in character_group. By default, characters in character_group are case sensitive. The "v" and "l" [first-last] character ranges in [^ aei] "avail": match any single character in the range from first to last. [bmurd] [bmurd] irds can match Birds, Cirds, Dirds. Wildcard: matches any single character except\ n.
To match the original meaning period character (. Or\ u002E), you must precede the character with an escape character (\.). The "ave" in A.E "have" and the "ate" in "mate"\ p {name} match any single character in the Unicode generic category or named block specified by name. The "C" and "L"\ P {name} in\ p {Lu} "City Lights" match any single character that is not in the Unicode generic category or named block specified by name. The "I", "t" and "y"\ w in\ P {Lu} "City" match any word character. \ w "R", "o", "m" and "1"\ W in "Room#1" match any non-word characters. The "#"\ s in\ W "Room#1" matches any white space characters. The "D"\ S in\ w\ s "ID A1.3" matches any non-white space characters. The "_"\ d in\ s\ S "int _ _ ctr" matches any decimal number. The "4"\ D in "4 = IV" matches any character that is not a decimal number. \ D "4 = IV" grouping construction of "", "=", "", "I" and "V"
The grouping construction describes the subexpression of the regular expression and is usually used to capture the substring of the input string.
The grouping construction describes pattern matching (subexpression) capturing matching subexpressions and assigning them to a zero-based sequence number. (\ w)\ 1 "ee" (?) in "deep"
< name >Subexpression) captures matching subexpressions into a named group. (?
< double>\ W)\ k
< double>"ee" in "deep" (?
< name1 -name2 >Subexpression) defines the balance group definition. ((? 'Open'\) [^\ (\)] *) + ((?' Close-Open'\)) [^\ (\)] *) * (? (Open) (?)) $"((1-3) * (3-1))" (?: subexpression) in "(1-3) * (3-1))". The "WriteLine" (? imnsx-imnsx:subexpression) in Write (?: Line)? "Console.WriteLine ()" applies or disables the options specified in subexpression. The zero widths of "A12xl" and "A12XL" (? = subexpression) in A\ d {2} (? I:\ W +)\ b "A12xl A12XL a12xl" are predicting advance assertions. \ W + (? =\.) "He is The dog ran. The sun is out." "is", "ran" and "out" (?! Subexpression) zero width negative prediction antecedent. The "sure" and "used" sub-expressions in\ b (?! un)\ w +\ b "unsure sure unity used" ("man" (? > subexpression) in "Hi woman Hi man" are not backtracking (also known as "greed"). The qualifiers "1ABB", "3ABB" and "5AB" in "1ABB 3ABBC 5AB 5AC" of [13579] (? > Atom B+)
The qualifier specifies how many instances of the previous element (which can be a character, group, or character class) must exist in the input string for a match to occur. Qualifiers include the language elements listed in the following table.
The qualifier describes pattern matching * matching the last element zero or more times. \ d *.\ d ".0", "219.9" + matches the previous element one or more times. "bee" in "be+"been", "be" in "bent"? Match the last element zero or once. " Rai?n "" ran "," rain "{n} match the last element exactly n times." ,\ d {3} "1043.6" matches the last element at least n times in "9876543210". \ d {2,} "166,29", "1930" {n, m} match the last element at least n times, but not more than m times. " "17668", "19302" * in "193024"? Match the previous element zero or more times, but as few times as possible. \ d ".0", "19.9", "219.9" +? Match the previous element one or more times, but as few times as possible. "be" in "be+?"been", "be" in "bent"? Match the previous element zero or once, but as few times as possible. "rai??n"ran", "rain" {n}? Match the leading element exactly n times. ",\ d {3}?" 043 "in" 1043.6 "," 876 "," 543 "and" 9876543210 "{n,}? Match the previous element at least n times, but as few times as possible. "\ d {2,}?" 166", "29" and "1930" {n, m}? The number of matches to the previous element is between n and m, but as few times as possible. " \ d {3pr 5}? "" 166", "17668" Verifying digits of n digits: ^\ d {n} $verifying at least n digits: ^\ d {n} $verifying numbers of Mmurn digits: ^\ d {m N} $verify zero and non-zero beginning numbers: ^ (0 | [1-9] [0-9] *) $verify positive real numbers with two decimal places: ^ [0-9] + (. [0-9] {2})? $verify positive real numbers with 1-3 decimal places: ^ [0-9] + (. [0-9] {1p 3})? verify integers and one decimal places: ^ [0-9] + (. [1-9]) ] {1})? $verify non-zero positive integers: ^\ +? [1-9] [0-9] * $verify non-zero negative integers: ^\-[1-9] [0-9] * $verify non-negative integers (positive integers + 0) ^\ dnumbers $verify non-positive integers (negative integers + 0) ^ (-\ d+) ^ (0 +) $verify characters with length 3: ^. {3} $verification consists of 26 English characters String of text letters: ^ [A-Za-z] + $verify a string of 26 uppercase letters: ^ [Amurz] + $verify a string of 26 lowercase letters: ^ [Amurz] + $verify a string of numbers and 26 letters: ^ [A-Za-z0-9] + $verify a string consisting of numbers, String consisting of 26 English letters or underscores: ^\ a-zA-Z $verify user password: ^ [letter]\ w {5pm 17} $correct format: start with a letter The length is between 6 and 18 and can only contain characters, numbers, and underscores. Verify that it contains characters such as ^% &',; =? $\ ": [^% &' =? $\ x22] + verify Chinese characters: ^ [\ u4e00 -\ u9fa5] + $Verification Email address: ^ (\ w) + (\.\ w +) * @ (\ w) + ((\.\ w {2jue 3}) {1jue 3}) $Verification InternetURL: ^ http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$ ^ [a-zA-z] +: / / (w + (- w +) *) (. (W + (- w +) *)) * (? S*)? $verify the phone number: ^ (\ d {3jue 4}\) |\ d {3jue 4} -)?-- the correct format is: XXXX-XXXXXXX,XXXX-XXXXXXXX,XXX-XXXXXXX,XXX-XXXXXXXX,XXXXXXX,XXXXXXXX. Verify ID number (15 or 18 digits): ^\ d {15} |\ d {18} |\ d {17} X $12 months of verification year: ^ (0? [1-9] | 1 [0-2]) the correct format is: 31 days of "01"-"09" and "1"12" verification month: ^ (0? [1-9]) | (1 | 2) [0-9]) | 30 | 31) the correct format of $is: 01, 09 and 1, 31. Integer: ^ -?\ dfloat $non-negative floating point number (positive floating point + 0): ^\ d + (\.\ d +)? $positive floating point ^ (([0-9] +. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9]. [0-9] +) | ([0-9] * [1-9] [0-9] *) $non-positive floating point Number (negative floating point number + 0) ^ ((-\ d + (\.\ d +)?) | (0 + (\ .0 +)?)) $negative floating point number ^ (- ((0-9) +. [0-9]. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] +) | ([0-9] * [1-9] [0-9] *) Floating point number ^ (-?\ d +) (\.\ d +)? a number between 0 and 100 At most two places after the decimal point ^ (?: (?! 0\ d)\ d {1Magne2} (?:\.\ d {1magin2})? | 100 (::\ .0 {1pc2})?) Verify the html tag /] + > / g III, code
Does it include a, b, c
Static void Main (string [] args) {/ / determines whether it contains the characters a, b, c string str = Console.ReadLine (); / / stores the characters entered by the user in str string regex = @ "[abc]" / / C # add @ "[abc]" to regular expression bool isMatch = Regex.IsMatch (str, regex); / / compare: IsMatch (character to be judged, regular): return Bool value Console.WriteLine (isMatch? "Match [abc]": "not Match [abc]"); / / output result Console.WriteLine ();}
Replace the beginning or end of a regular
Static void Main (string [] args) {StringBuilder s = new StringBuilder ("www.baidu.com", 50); / / declares a string with a capacity of 50 / / news= regular class. Replacement string (string to be processed, regular, added)-replace the regular position with the following string / / the beginning of the string string news = Regex.Replace (s.ToString (), "^", "URL:"); / / ^: represents the beginning of Console.WriteLine (news) / / output / / replace the ending news = Regex.Replace (s.ToString (), "$", "ending"); / / $: represents the ending Console.WriteLine (news); Console.ReadLine ();}
That begins and ends with any character other than letters, underscores, and numbers.
Static void Main (string [] args) {string s = Console.ReadLine (); / / s receives user input string regex = @ "^\ words $"; / / regular: bool isMatch = Regex.IsMatch (s, regex) beginning and ending with any character other than letters, underscores, numbers. / / match user input, whether the regular condition Console.WriteLine (isMatch) is met? "satisfied": "not satisfied"); / / trinomial operation Console.WriteLine ();}
Replacement character
Static void Main (string [] args) {string s = "abcdef"; string regex = @ "[^ bde]"; / / [^ bde] represents all characters except b, d, e, any character string newReplace = Regex.Replace (s, regex, "1") / / match the characters in s except bde, replace them with 1 Console.WriteLine (newReplace); Console.WriteLine ();}
Match QQ number
Static void Main (string [] args) {string qq = Console.ReadLine (); / / wait for the user to enter string regex = @ "^\ d {5jue 11} $"; / / regular: the beginning and end are all numbers, and 5-11 digits are numbers bool isqq = Regex.IsMatch (qq, regex); / / determine and return Boolean Console.WriteLine (isqq? "Yes QQ number": "not QQ number"); / / trinomial operation Console.WriteLine ();}
Verify IP addr
Static void Main (string [] args) {string regex = @ "^ ([1]?\ d\ d? | 2 [0-4]\ d | 25 [0-5]) {3} ([1]?\ d\ d? | 2 [0-4]\ d | 25 [0-5])) $"; / / determine whether the IP address is compliant with while (true) {string s = Console.ReadLine () / / wait for the user to enter bool isMatch = Regex.IsMatch (s, regex); / / verify whether ip is legal Console.WriteLine (isMatch? "is the IP address": "not the IP address"); / / ternary operation}} above is about "what are the knowledge points of C# regular expressions?". I believe you all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.