. Net regular expression

Source: Internet
Author: User
1. Regular expressions are used for text processing and are language independent. They are implemented in almost all languages. Javascript is also used. 2. A regular expression is a text mode consisting of common characters and special characters (called metacharacters. This mode describes one or more strings to be matched when searching the text subject. A regular expression is used as a template to match a character pattern with the searched string. 3. Like the wildcard "*. JPG "and" % AB % ", which are special strings for string matching. 4. Regular Expressions are very complicated and do not want to be mastered at once, understand what a regular expression can do (string matching, string extraction, string replacement), Master common Regular Expression usage, and use it for further query. 5. Highlights of job search. Regular Expressions are also involved in collectors, sensitive word filtering, urlrewite, and validator in subsequent projects.

6. Regular Expressions operate on strings.

Metacharacters
To learn regular expressions, understanding metacharacters is a challenge that must be overcome. Do not remember

1..: match any single character except \ n. For example, the regular expression "B. g can match the following strings: "big", "Bug", and "BG", but it does not match "buug", "B .. g can match "buug ". 2. []: match any character in the brackets. For example, the regular expression "B [AuI] G" matches bugs, big, and bag, but does not match beg or Baug. You can use the hyphen "-" in brackets to specify the character range to simplify the representation. For example, a regular expression [0-9] can match any number character, in this way, the regular expression "A [0-9] C" is equivalent to "A [0123456789] C" and can match strings such as "a0c", "A1c", and "a2c; you can also specify multiple intervals. For example, [A-Za-Z] can match any uppercase/lowercase letter, and [A-Za-z0-9] can match any uppercase/lowercase letter or number. 3. Perform logical "or" operations on the two matching conditions. 'Z | food' can match "Z" or
"Food ". '(Z | f) Ood' matches "zood" or "food ". // Pay attention to the ^ $ problem. 4. (): Set ()
The enclosed expression is defined as a "group" and the characters matching the expression are saved in a temporary area. This metacharacter is very useful for string extraction. Represents some characters as a whole. Change the priority and define the extraction group. 5. *: match up to 0 subexpressions before it. It does not matter with the wildcard. For example, the regular expression "zo *" (equivalent to Z (o) *) can match "Z", "zo", and "Zoo". Therefore, ". * "means that any string can be matched. "Z (B | C) *" → ZB, zbc, zcb, zccc, zbbbccc. "Z (AB) *" can match Z, Zab, and zabab (use parentheses to change the priority ). 6. +: match the previous subexpression once or multiple times, and compare it with * (0 to multiple times ). For example, the regular expression 9 + matches 9, 99, and 999. "Zo +" can match "zo" and "Zoo", but cannot match "Z ". 7 ,? : Match the previous subexpression zero or one time. For example, "Do (ES )?" Can match "do" or
"Does ". It is generally used to match "optional parts ". (To terminate the greedy mode) 8. {n}: match the specified n times. "Zo {2}" → Zoo. For example, "E {2}" cannot match "E" in "bed", but can match two "E" in "seed ". // Seeeed, no. 9. {n ,}: match at least N times. For example, "E {2,}" cannot match "E" in "bed", but can match all "E" in "seeeeeeeed ". 10, {n, m}: match at least N times and at most m
Times. "E {}" will match the first three "E" in "seeeeeeeed ". {2, 5} // bed, seed, seeed; beeeeed error. Qualifier: specifies the number of occurrences of the regular expression. 1. ^ (SHIFT + 6): match the beginning of a row. For example, the regular expression "^ RegEx" can match the start of the string "RegEx I will use", but cannot match "I will use RegEx ". 2. ^ another meaning: No! ([^ 0-9]) 3. $: match the row Terminator. For example, the regular expression "floating cloud $" can match the end of the string "Everything is floating cloud", but cannot match the string "floating cloud"
4. ^ ABC, matching the start of a regular expression • abcjflkdsjfkdsjf888 $, matching the end of a regular expression. • Number of credits happy liberation intersection Mobile Phone Fee 888 shorthand expressions note that these shorthand expressions do not consider escape characters, here \ represents the character \, rather than the C # string Level \, in C # code, use @ or \ double escape. Differentiate the transfer at the C # level and the transfer at the regular expression level, just as the escape characters of the C # And the regular expression are both. The escape of the regular expression is after C # (layer-by-layer exploitation ). Think of the Escape Character of C # As %. In C #'s view, @ "\-" is a normal string, but it has a special meaning in the regular expression analysis engine. "\ D" or @ "\ D" • \ D: represents a number, equivalent to [0-9] \ D → \ D • \ D: represents a non-number, equivalent to [^ 0-9] • \ s: blank characters such as line breaks and Tab tabs (space, carriage return, and Tab) • \ s: represents non-blank characters (A0% $ @) • \ W: Match letters, numbers, underscores, or Chinese characters, that is, the characters that can constitute a word, except % &#@! $ And other characters. • \ W: Non \ W, equivalent to [^ \ W] D: Digital; s: space, W: Word. In upper case, the "non" regular expression is in. net is represented by a string. This string format is special. No matter how special it is, it seems to be a common string in the C # language. The specific meaning of this string is analyzed by the RegEx class. How do I match strings greater than 10 and less than 20? (A regular expression is a string operation .) ^ [1] [1-9] $, [11, 12, 13, 14, 15, 16, 17, 18, 19] observe the string! Observe the string carefully before writing the regular expression. Regular Expressions (regularexpression) are mainly used in four common RegEx situations: (C # syntax) • determine whether to match: RegEx. ismatch ("string", "regular expression"); • string extraction: RegEx. match ("string", "Regular Expression of the string to be extracted"); // only one (extracted once) • string extraction (extracted cyclically): RegEx. matches (), (all matching strings can be extracted .) • String replacement: RegEx. Replace ("string", "regular", "replace content"); The RegEx. ismatch method is used to determine whether a string matches a regular expression. String Matching example:

RegEx. ismatch ("bbbbg", "^ B. * G $ ");

RegEx. ismatch ("BG", "^ B. * G $ ");

RegEx. ismatch ("Gege", "^ B. * G $ ");

You must not forget ^ and $. Otherwise, you can match yesbagit.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.