Python Regular Expression usage tutorial

Source: Internet
Author: User

Python regular expressions are easy to use,

Metacharacters

1. Match metacharacters:

. It matches any character except the line feed character. In alternate mode (re. dotaLL), it can even match the line feed.
^ Match the beginning of a row. Unless the MULTILINE flag is set, it only starts to match the string.
$ Matches the end of a line. The end of a line is defined as either the end of a string or any position after a newline character.
[] They are often used to specify a character category. The character category is a character set that you want to match.
\ The backslash can be followed by different characters to indicate different special meanings. It can also be used to cancel all metacharacters so that you can match them in the mode.
| Optional, or the "or" operator.
() Indicates a remembered group ). You can use the re. search function to return the groups () function of the object to obtain its value.

2. Repeated metacharacters:

* Repeated 0 or N times
+ Repeat 1 or N times
? Repeat 0 or 1 times
{M, n} the qualifier indicates at least m duplicates, up to n duplicates.

3. pre-defined character set starting "\"

\ D matches any decimal number; it is equivalent to the class [0-9].
\ D matches any non-numeric character; it is equivalent to the class [^ 0-9].
\ S matches any blank character; it is equivalent to class [\ t \ n \ r \ f \ v].
\ S matches any non-blank characters. It is equivalent to the class [^ \ t \ n \ r \ f \ v].
\ W matches any alphanumeric character; it is equivalent to the class [a-zA-Z0-9 _].
\ W matches any non-alphanumeric character; it is equivalent to the class [^ a-zA-Z0-9 _].
\ A only matches the first string. In the MULTILINE mode, \ A and ^ are actually the same. However, they are different in MULTILINE mode; \ A only matches the first character string, and ^ can also match any position of the character string after the linefeed.
\ Z only matches the end of the string.
\ B word boundary. This is a zero-width assertions which is used only to match the beginning and end of a word. A word is defined as a sequence of letters and numbers. Therefore, the end of a word is indicated by a blank or non-alphanumeric character.
\ B is another zero-width assertions operator, which is opposite to \ B and matches only when the current position is not at the word boundary.

Note: [akm $] matches the character "a", "k", "m", or any one of "$"; "$" is usually used as a metacharacter, however, in the character category, its features are removed and restored to common characters.

Common RegexObject (re) object Methods

MatchObject = re. match (pattern, string, flag = '0'): determines whether the RE matches at the starting position of the string.
MatchObject = re. search (pattern, string, flag = '0'): scan the string and find the matching position of this RE.
Tuple = re. findall (pattern, string, flag = '0'): Find all substrings matching the RE and return them as a list
MatchObjectList = re. finditer (pattern, string): finds all substrings matching the RE and returns them as an iterator.

Common MatchObject Methods

Findstring = matchObject. group (): returns the string matched by the RE.
I = matchObject. start (): returns the starting position of the match.
I = matchObject. end (): returns the position at which the match ends.
Start, end = matchObject. span (): returns the position where a tuple contains a match (start, end ).

Compilation flag

DOTALL, S: Make. Match All characters including line breaks
IGNORECASE, I: Make the matching case insensitive
LOCALE, L: Local identification (locale-aware) Matching
MULTILINE, M: MULTILINE matching, affecting ^ and $
VERBOSE, X: the verbose status of REs can be used to make it clearer and easier to understand.

Experience

1. compile and use: re. compile ('pattern ')

2. Do not compile it repeatedly: Put it into the global space or cell space, and take an instance out of a closure instance.

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.