The regular expression of Python

Source: Internet
Author: User

1)

Match multiple regular expressions with pipe symbol (|)

Example

at | Home match at, home

2)

Match any single character (.)

Example

F.O matches any character in the middle of "F" and "o", such as FAO, F9O, F#O, etc.

3)

Match from beginning or end of character wear or word boundary (^/$/\b\b)

\b Matching pattern is a word boundary, the corresponding pattern must be at the beginning of a word;

\b Matches only the pattern that appears in the middle of a word, the character that is not on the boundary of a word.

Example

^from matches any string starting with from

/bin/tcsh$ matches any string ending with/bin/tcsh

^subject:hi$ matches a string consisting only of Subject:hi

Any string containing the "the"

\bthe any string starting with "the"

\bthe\b matches only the word "the"

4) Create character class ([])

A regular expression that uses square brackets matches any one of the characters in the square brackets.

Square brackets are only logical or functional.

Example:

B[aeiu]t Match string bat, bet, bit, but

[CR] [23] [DP] [O2] Match string has 16 combinations, first [CR] Select one, then [23] Select one, ..., and finally [O2] choose one.

5) Specify range (-) and negation (^)

A hyphen (-) in the middle of a pair of symbols in square brackets is used to denote a range of characters. such as [0-9], match the decimal number

The first character after the left parenthesis is the up arrow symbol (^), which indicates that any character in the character set is not matched. such as [^aeiou], match a non-vowel character

6) Use the closure operator (*, +,? {}) implements multiple occurrences/repeat matches

*: Match it to the left of the regular expression appears 0 or more times 0 times;

+: Matches at least one occurrence of the regular expression on its left;

?: Match it to the left of the regular expression pattern appears 0 or one time.

{}: If {m} represents a match for m occurrences, {m,n} indicates a match of M to N occurrences.

[Dn]ot? The match has a maximum of one character "T", i.e. do, no, dot, not.

[0-9] {15, 16} matches 15 or 16 digits, such as credit card number

</? [^>]+> matches all valid HTML tag strings, i.e. <...>, </...>.

7) Special characters

Some special characters can be used to represent the character set.

\d: expression [0-9]

\w: means [a-za-z0-9]

\s: denotes whitespace characters

\d: A character that represents a non-decimal number, that is, [^0-9]

Example:

\w+-\d+ represents a string of letters and numbers and at least one number, for example: ABCD-9

\D{3}-\D{3}-\D{4} such as 800-555-1212

\[email protected]\w+\.com for example [email protected]

8) Use parentheses (()) to assemble the group

Not only do we want to know if the entire string matches the regular expression, but also want to remove a particular string or substring when the match succeeds.

() function:

Grouping regular Expressions

Matching subgroups

Example:

m = Re.match (' (A (b)) ', ' AB ') two sub-groups

M.group () All matching parts

M.group (1) Matched sub-group 1 output ' AB '

M.group (2) Matched sub-group 2 output ' B '

M.groups () tuple output of all matched subgroups (' AB ', ' B ')

The regular expression of Python

Related Article

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.