Regular Expressions (Regular Express) Learning notes

Source: Internet
Author: User
Tags character classes character set numeric regular expression

1. matches any single character . (Full period) metacharacters

1.1. Multiple points match any number of characters
1.2. If you only want to match the. character, you can use the escape character \.

2.      match one of several characters [] Meta-character

2.1. Eg:[ns]a matching na or SA

2.2. Using [] match character interval eg: [a-za-z0-9]

2.3-(hyphen) is a special character that can be used only in [] to denote hyphens, in places other than the character set,-just an ordinary character and not be escaped.

2.4. The character range can only be small to large

3. take a non-matching ^ meta character

3.1. Except for the character in the character set, the other characters can match.

3.2. Eg:[ns]a[^0-9]\\.xls match Nam.xls, mismatch Sa8.xls

4. match a specific character class

character class meaning of the character class an equivalent regular expression
\d Any one numeric character [0-9]
\d Any one non-numeric character [^0-9]
\w Any number, letter, or underscore [A-za-z0-9_]
\w Any one character except numbers, letters, and underscores [^a-za-z0-9_]
\s Any one blank character [\f\n\r\t\v]
\s Any one non-whitespace character ' [^\f\n\r\t\v] '

Note: JavaScript does not support the use of POSIX character classes in regular expressions (eg [: alnum:] denotes any one letter or number)

5. match one or more character sets followed by the + metacharacters

5.1. The metacharacters used in the character set [] are interpreted as normal characters and do not need to be escaped, but there is no harm in escaping.

6. match 0 or more characters character set followed by * meta characters

7.      match 0 or one character character set followed by. Metacharacters

8.      set an exact value for the number of repetitions      {\d}      (eg \d{6} matches a six-digit number) {} meta-character

9.        set a range for the number of repeat matches {\d,\d}

10.      match "at least how many times" {\d,}

Greedy type metacharacters and lazy type metacharacters (as the name implies, one match as many as possible, the other matches as few as possible, and lazy characters are used to prevent over-matching . )

greedy type meta character lazy type meta characters
* \*。
+ \+?
{N,} {N,}?

11. Location Matching

11.1. Word boundary \b Words with spaces or hyphens before and after matching (words can be letters, numbers, or underscores) (eg: \bcat\b matches cat,-cat-, mismatch scattered)

11.1.1. Non-word boundary \b matches a non-word (non-word can be a space or a hyphen) before and after matching a space or hyphen

11.2. string Bounds

string Bounds meaning of the string boundary Example Example Explanation
^ The position used to determine the beginning of the string ^\s*< xml.*> Matches a string beginning with < xml...>
$ The position used to determine the end of the string </[hh][tt][mm][ll]>\s*$ Matches a string ending with

11.3.       Branch Matching mode (? m)

Eg: (? m) ^\s*//.*$ matches all comments that are in a separate line

12.       Sub-expression ()

13. Backtracking reference match \\d to be used with sub-expressions (), where \d represents the \d sub-expression in re

(JavaScript uses $ instead of \, or $\d) \d to represent any number

eg

<[hH][1-6]>.*?</[hH][1-6]>//May match

<[HH] ([1-6]) >.*?</[hH]\1>//Backtracking reference \1 matches the first subexpression, that is, the same part as [1-6], the representation of JS is <[hh] ([1-6]) >.*?</[hh]$1 >

14. The front and back lookup is used to match the correct position, but some parts of the regular expression are not part of the matching result and are not returned. (Common regular expressions support forward lookups, but there is not that much support for backward lookups.) Java,.net,php,perl supports backward lookups (but there are some limitations), and javascript,coldfusion does not support backward lookups. )

search type before and after expressions for finding types before and after Example Example Explanation
Search forward (?=) .+(?=:) Match: Previous characters, but not including:
Find Backward (? <=) (? <=\$) [0-9]+ Matches the character after $, but does not include the $
Negative Backward Lookup (? <!) \b (? <!\$) [0-9]+\b Eg:i paid $ apples. Match 100, which does not match 30 in $.
Negative Backward Lookup (?!)

15. Embedding Conditions
15.1.      Feedback Reference Conditions (? (backreference) True-regex|false-regex) where backreferrence is a feedback reference

Eg: (<[aa]\s+[^>]+>\s*)?<[ii][mm][gg]\s+[^>]+> (? ( 1) \s*</[aa]>)

The

matches the individual tag or the entire link label that is enclosed between <a...> and </a>.

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.