Python Regular Expression Guide

Source: Internet
Author: User
Tags aliases instance method locale locale setting

This article describes Python's support for regular expressions, including the basics of regular expressions and the complete introduction and usage examples of the Python regular Expression standard library. The content of this article does not include how to write efficient regular expressions, how to optimize regular expressions, and see other tutorials for these topics.

Note: This article is based on Python2.4; if you see a word you don't understand, please remember Baidu Google or wiki, whatever.

1. The regular expression base 1.1. Simple Introduction

Regular expressions are not part of Python. Regular expressions are powerful tools for working with strings, with their own unique syntax and an independent processing engine, which may not be as efficient as Str's own approach, but very powerful. Thanks to this, in the language that provides the regular expression, the syntax of the regular expression is the same, except that the number of grammars supported by different programming languages is different; but don't worry, the unsupported syntax is usually the less common part. If you've already used regular expressions in other languages, simply take a look and get started.

Shows the process of matching using regular expressions:

The approximate matching process for regular expressions is to take out the expression and compare the characters in the text, and if each character matches, the match succeeds; If a match is unsuccessful, the match fails. If there are quantifiers or boundaries in an expression, the process can be slightly different, but it is also well understood, with examples of fancy and a few more times you can understand them.

Lists the regular expression meta characters and syntax supported by Python:

1.2. Greedy mode and non-greedy mode of counting quantifiers

Regular expressions are typically used to find matching strings in text. The number of words in Python is greedy by default (which may be the default non-greedy in a few languages), always trying to match as many characters as possible, and not greedy, instead, always trying to match as few characters as possible. For example: the regular expression "ab*" will find "abbb" if it is used to find "ABBBC". And if you use a non-greedy quantity word "ab*?", you will find "a".

1.3. Anti-slash puzzle

As with most programming languages, "\" is used as an escape character in regular expressions, which can cause a backslash to be plagued. If you need to match the character "\" in the text, then 4 backslashes "\\\\" will be required in the regular expression expressed in the programming language: the first two and the last two are used to escape the backslash in the programming language, converted to two backslashes, and then escaped in the regular expression into a backslash. The native string in Python solves this problem well, and the regular expression in this example can be expressed using R "\ \". Similarly, a "\\d" that matches a number can be written as r "\d". With the native string, you no longer have to worry about missing the backslash, and the expression is more intuitive.

1.4. Matching mode

Regular expressions provide some matching patterns that are available, such as ignoring case, multiline matching, and so on, which is described in the factory method Re.compile (pattern[, flags]) of the pattern class.

2. Re module 2.1. Start using RE

Python provides support for regular expressions through the RE module. The general step for using re is to compile the string form of the regular expression into a pattern instance, then use the pattern instance to process the text and get the matching result (a match instance), and finally use the match instance to get the information and do other things.

# Encoding:utf-8import re# compiles the regular expression into the pattern object pattern = Re.compile (R ' Hello ') # matches the text with pattern, gets the match result, the mismatch returns Nonematch = Pattern.match (' Hello world! ') If match:    # use Match to get grouping information    print match.group () # # # output # # # # Hello

Re.compile (strpattern[, flag]):

This method is the factory method of the pattern class, which compiles a regular expression in the form of a string into a pattern object. The second parameter, flag, is the matching pattern, and the value can use the bitwise OR operator ' | ' To take effect at the same time, such as re. I | Re. M. Alternatively, you can specify patterns in the Regex string, such as re.compile (' pattern ', re. I | Re. M) is equivalent to Re.compile (' (? im) pattern ').
The optional values are:

    • Re. I (Re. IGNORECASE): Ignore case (full notation in parentheses, same as below)
    • M (MULTILINE): Multiline mode, changing the behavior of ' ^ ' and ' $ ' (see)
    • S (dotall): Point any matching pattern, change '. ' The behavior
    • L (LOCALE): Make a predetermined character class \w \w \b \b \s \s depends on the current locale setting
    • U (Unicode): Make a predetermined character class \w \w \b \b \s \s \d \d Depending on the character attributes of the UNICODE definition
    • X (VERBOSE): Verbose mode. In this mode, the regular expression can be multiple lines, ignore whitespace characters, and can be added to comments. The following two regular expressions are equivalent:
A = Re.compile (r "" "\d +  # the integral part                   \.    # The decimal point                   \d *  # some fractional digits "", Re. X) b = Re.compile (r "\d+\.\d*")

Re provides a number of modular methods for completing regular expression functions. These methods can be substituted with the corresponding method of the pattern instance, with the only advantage being that one less line of Re.compile () code is written, but the compiled pattern object cannot be reused at the same time. These methods are described in the example Method section of the pattern class. As the above example can be abbreviated as:

m = Re.match (R ' Hello ', ' Hello world! ') Print M.group ()

The RE module also provides a method of escape (string), which is used to such as the regular expression metacharacters in string */+/, and so on before the escape character is returned, which is a bit more useful when a large number of matching metacharacters are required.

2.2. Match

The match object is a matching result that contains a lot of information about this match and can be obtained using the readable properties or methods provided by match.

Property:

    1. string: The text to use when matching.
    2. re: The pattern object to use when matching.
    3. POS: The index in which the text expression begins the search. The value is the same as the parameter with the same name as the Pattern.match () and Pattern.seach () methods.
    4. endpos: The index of the end-of-search text expression. The value is the same as the parameter with the same name as the Pattern.match () and Pattern.seach () methods.
    5. lastindex: The index of the last captured grouping in the text. If there are no captured groupings, it will be none.
    6. Lastgroup: The alias of the last captured group. If the group has no aliases or no captured groupings, it will be none.

Method:

    1. Group ([Group1, ...]):
      Gets the string that is intercepted by one or more groups, and when multiple parameters are specified, they are returned as tuples. Group1 can use numbers or aliases; number 0 represents the entire matched substring; returns Group (0) when no parameters are filled; Groups that have not intercepted a string return none; The group that intercepted multiple times returns the last substring intercepted.
    2. groups ([default]):
      Returns the string intercepted by all groups as a tuple. Equivalent to calling group (,... last). Default indicates that a group that does not intercept a string is replaced with this value, which defaults to none.
    3. groupdict ([default]):
      Returns the alias of the group with the alias as the key, the substring intercepted by the group as the value of the dictionary, the group without the alias is not included. The default meaning is the same.
    4. Start ([group]):
      Returns the starting index of the substring intercepted by the specified group in string (the index of the first character of the substring). The group default value is 0.
    5. End ([group]):
      Returns the end index of the substring intercepted by the specified group in string (the index of the last character of the substring + 1). The group default value is 0.
    6. span ([group]):
      Returns (Start (group), End (group)).
    7. Expand (Template):
      Substituting the matched grouping into the template and then returns. The template can be grouped using \id or \g<id>, \g<name> reference, but cannot use number 0. \id and \g<id> are equivalent, but \10 will be considered a 10th grouping, if you want to express \1 after the character ' 0′, you can only use \g<1>0.
Import rem = Re.match (R ' (\w+) (\w+) (? p<sign>.*) ', ' Hello world! ') Print "m.string:", M.stringprint "M.re:", M.reprint "M.pos:", M.posprint "M.endpos:", M.endposprint "M.lastindex:", M.lastindexprint "M.lastgroup:", M.lastgroupprint "M.group:", M.group (1, 2) print "M.groups ():", m.groups () print " M.groupdict (): ", M.groupdict () print" M.start (2): ", M.start (2) print" M.end (2): ", M.end (2) print" M.span (2): ", M.span (2) Print R "M.expand (R ' \2 \1\3 '):", M.expand (R ' \2 \1\3 ') # # # output # # # # # # M.string:hello world!# m.re: <_sre. Sre_pattern object at 0x016e1a38># m.pos:0# m.endpos:12# m.lastindex:3# m.lastgroup:sign# m.group: (' Hello ', ' World ') # m.groups (): (' Hello ', ' world ', '! ') # m.groupdict (): {' sign ': '! '} # M.start (2): 6# m.end (2): 11# M.span (2): (6, one) # M.expand (R ' \2 \1\3 '): World hello!
2.3. Pattern

The pattern object is a compiled regular expression that can be matched to the text by a series of methods provided by pattern.

Pattern cannot be instantiated directly and must be constructed using Re.compile ().

The pattern provides several readable properties for getting information about an expression:

    1. Pattern: The expression string used at compile time.
    2. Flags: The matching pattern used at compile time. Digital form.
    3. Groups: The number of groupings in an expression.
    4. Groupindex: The alias of the group with the alias in the expression is the key, the dictionary with the number corresponding to that group, and the group without the alias is not included.
Import rep = Re.compile (R ' (\w+) (\w+) (? p<sign>.*) ', Re. Dotall) print "P.pattern:", P.patternprint "P.flags:", P.flagsprint "p.groups:", P.groupsprint "P.groupindex:", p.groupindex### Output # # P.pattern: (\w+) (\w+) (? p<sign>.*) # p.flags:16# p.groups:3# p.groupindex: {' sign ': 3}

instance method [| Re module Method]:

1.match (string[, pos[, Endpos]) | Re.match (pattern, string[, flags]):
This method attempts to match pattern from the POS subscript of string, returns a match object if pattern is still matched at the end, or none if pattern does not match during the match, or if the match does not end with Endpos.
The default values for POS and Endpos are 0 and Len (string), Re.match () cannot specify these two parameters, and the parameter flags specifies the matching pattern when compiling pattern.
Note: This method is not an exact match. If the string has any remaining characters at the end of the pattern, it is still considered successful. If you want an exact match, you can add the boundary match ' $ ' at the end of the expression.
See section 2.1 for an example.

2.search (string[, pos[, Endpos]) | Re.search (pattern, string[, flags]):
This method is used to find substrings in a string that can match a success. Attempts to match the pattern from the POS subscript of string, returns a match object if the pattern ends with a match, and tries to match the POS after 1 if it does not match, and returns none until Pos=endpos is still not matched.
The default values for POS and Endpos are 0 and Len (string), and Re.search () cannot specify both parameters, and the parameter flags specify a matching pattern when compiling pattern.

# Encoding:utf-8 Import Re # compiles a regular expression into a pattern object pattern = Re.compile (R ' World ') # Use Search () to find a matching substring, no matching substring will return none # this The example using match () does not match successfully with match = Pattern.search (' Hello world! ') if match:     # use Match to get grouping information     print match.group () # # # output # # # # World

3.split (string[, maxsplit]) | Re.split (Pattern, string[, Maxsplit]):

Returns a list after splitting a string by a substring that can be matched. The maxsplit is used to specify the maximum number of splits and does not specify that all will be split.

Import rep = Re.compile (R ' \d+ ') print p.split (' One1two2three3four4 ') # # # # # # # [' One ', ' one ', ' one ', ' I ', ' three ', ' four ', ']

4.findall (string[, pos[, Endpos]) | Re.findall (pattern, string[, flags]):

Searches for a string, returning all matching substrings as a list.

Import rep = Re.compile (R ' \d+ ') print p.findall (' One1two2three3four4 ') # # # output # # # # [' 1 ', ' 2 ', ' 3 ', ' 4 ']

5.finditer (string[, pos[, Endpos]) | Re.finditer (pattern, string[, flags]):

Searches for a string that returns an iterator that accesses each matching result (match object) sequentially.

Import rep = Re.compile (R ' \d+ ') for M in P.finditer (' One1two2three3four4 '):    print M.group (), # # # output # 1 2 3 4

6.sub (REPL, string[, Count]) | Re.sub (Pattern, REPL, string[, Count]):

Returns the replaced string after each matched substring in string is replaced with REPL.

When Repl is a string, you can use \id or \g<id>, \g<name> reference grouping, but you cannot use number 0.
When Repl is a method, this method should only accept one parameter (the match object) and return a string for substitution (the returned string cannot be referenced in the grouping).

Count is used to specify the maximum number of replacements, not all when specified.

Import rep = Re.compile (R ' (\w+) (\w+) ') s = ' I say, hello world! ' Print p.sub (R ' \2 \1 ', s) def func (m):    return M.group (1). Title () + "+ m.group (2)." Title () print P.sub (func, s) # # # OUTPU t # # # # Say I, World hello!# I say, hello world!

7.subn (REPL, string[, Count]) |re.sub (pattern, REPL, string[, Count]):

Returns (Sub (REPL, string[, Count]), number of replacements).

Import rep = Re.compile (R ' (\w+) (\w+) ') s = ' I say, hello world! ' Print p.subn (R ' \2 \1 ', s) def func (m):    return M.group (1). Title () + "+ m.group (2)." Title () print P.subn (func, s) # # # Out Put # # # # # # # (' Say I, World hello! ', 2) # (' I say, hello world! ', 2)

The above is Python support for regular expressions. Mastering regular expressions is a skill that every programmer must have, and there are no programs that do not deal with strings these days. The author is also in the initial stage, with June encouragement, ^_^

In addition, the special structural part of the figure does not cite examples, the use of these regular expressions is a certain difficulty. It's interesting to think about how to match words that don't start with ABC, ^_^

Python Regular Expression Guide

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.