Python Regular Expressions and python Regular Expressions

Source: Internet
Author: User

Python Regular Expressions and python Regular Expressions
1. Regular expression syntax

A regular expression can specify a pattern to match the corresponding string.

Special characters either represent common characters of a category or affect the interpretation of the regular expressions around them. The regular expression pattern string cannot contain null bytes, but can be used\ NumberThe symbol specifies the Null Byte, for example

'\ X00'.

Special characters include:

"." In default mode, match any character except line breaks. If the re. DOTALL flag is specified, any character including the line break is matched.

"^" Matches the start position of the string in the default mode. The re. MULTILINE flag is specified and matches the character after the line break. For example, if the regular expression '^ py' matches the string 'py88py', only one 'py' can be obtained'

'$' Matches the end of the string in the default mode. The re. MULTILINE flag is specified and matches the position before the line break.

"*" Matches zero or multiple leading mode strings as many as possible. 'AB *' can match 'A' 'AB' abb ...... (As many matches as possible )'

"+" Matches one or more leading mode strings and matches as many strings as possible. 'AB +' can match 'abb' abbb ...... 'As many as possible

"?" Matches zero or multiple leading mode strings. 'AB? 'Can match 'A' AB'

"*?" "+ ?" "?" The delimiters are greedy and match as many texts as possible. After the qualifier, add? The match will be performed in a non-greedy or minimal manner.

"{M}" indicates exact matching of the previous regular expression m copies. A small number of matches will cause the entire expression to not match. For example, a {6} will precisely match 6 'A' characters, otherwise it will not match.

The regular expression generated by "{m, n}" matches m to n duplicates of the leading regular expression, and tries to match as many duplicates as possible. For example, a {3, 5} matches 3 to 5 characters a. Omitting m indicates that the lower bound is 0, and omitting n indicates that the upper bound is infinite.

"{M, n }?" For a 6-character string 'aaaaa', a {3, 5} will match 5 a, a {3, 5 }? 3 a will be matched.

"\" Is used to escape any special character, and can match the character '*''? 'And so on

"[]" Is used to represent a character set. In a collection:

  • Characters can be listed one by one. For example, [amk] can match the character 'a' m' 'K'
  • The two characters are separated by '-'. For example, '[a-z]' matches any character from 'A' to 'Z. If '-' is escaped, or 'a-'or'-A', it will match a single '-' character.
  • Within the set, special characters will lose their special meaning. For example, [(* +] matches the character '(' * '+'
  • Character classes are also accepted in the collection, such as \ w or \ s, although the matching characters depend on whether re. LOCAL or re. UNICODE mode is mandatory.
  • Character that is no longer within a certain range can be matched by a complementary set. If the first character of the set is '^', all characters not in the set will be matched. If '^' is not the first character in the Set, it has no special meaning.
  • To match the character ']', you can put a backslash in front of it or put it at the beginning of the set.

"|" A | B. Here, A and B can be any regular expressions. The created regular expression either matches A or B, '|' can be used to separate any number of regular expressions.

 

 

2. re Module Content

The re module defines several functions, constants, and exceptions.

Re. compile (pattern, flag = 0) compiles the regular expression pattern into a regular expression object, which can be matched using the match () and search () methods.

 

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.