Python3 Regular Expression Guide

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

1. Regular Expression Basics 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 编译正则表达式

The RE module provides the Re.compile () function to compile a string into the pattern object for matching or searching. The function prototypes are as follows:

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. IGNORECASE : Ignores case, same as re. I .

    • re. MULTILINE : Multiline mode, change ^ and $ behavior, same as re. M .

    • re. Dotall : Point any matching pattern, let '. ' You can match any character, including ' \ n ', with re. S .

    • re. Locale : Make the predefined character class \w \w \b \b \s \s depends on the current locale, same as re. L .

    • re. ASCII : Make \w \w \b \b \s \s match only ASCII characters, not Unicode characters, same as re. A .

    • re. VERBOSE : Verbose mode. In this mode, the regular expression can be multiple lines, ignore whitespace characters, and can be added to comments. The main purpose is to make regular expressions easier to read, with re. X . For example, the following two regular expressions are equivalent:

    A = Re.compile (R"" "\d +  # the integral part                        \.    # The decimal point                        \d *  # some fractional digits"", re. X)      = 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.

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:

string: The text to use when matching.

re: The pattern object to use when matching.

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.

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.

lastindex: The index of the last captured grouping in the text. If there are no captured groupings, it will be none.

Lastgroup: The alias of the last captured group. If the group has no aliases or no captured groupings, it will be none.

Method:

Group ([Group1, ...]):
Gets the string that is intercepted by one or more groups, and returns a tuple when multiple parameters are specified. 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.

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.

Groupdict ([default]):
Returns a dictionary with aliases for the alias of the group, the value of the substring intercepted by the group, and no alias for the group. The default meaning is the same.

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.

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.

span ([group]):
Returns (Start (group), End (group)).

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 ', use only \g<1>0.

Examples are as follows:

ImportREM= 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)PrintR"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): One#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:

    • Pattern: The expression string used at compile time.
    • Flags: The matching pattern used at compile time. Digital form.
    • Groups: The number of groupings in an expression.
    • Groupindex: The alias of the group that has an alias in the expression is the key, the dictionary with the number corresponding to the group, and the group without the alias is not included
ImportRep= 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]:

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.

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-8ImportRe#compiling a regular expression into a pattern objectPattern = Re.compile (r' World')  #use Search () to find a matching substring, no matching substring will be returned when none is present#using Match () in this example does not match successfullyMatch = Pattern.search ('Hello world!')  ifmatch:#use match to get grouped information    PrintMatch.group ()## # output # # ## World

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= re.compile (R'\d+')print p.split (' ONE1TWO2THREE3FOUR4 '  ## # # # # ##  [' One ', ' One ', ' three ', ' four ', ']

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

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

Import= re.compile (R'\d+')print p.findall (' ONE1TWO2THREE3FOUR4 '  ## #Output# ## #  [' 1 ', ' 2 ', ' 3 ', ' 4 ']

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   re p  = re.compile (R " \d+  "  )  for  m in  P.finditer ( '  one1two2three3four4   "  print  Span style= "COLOR: #000000" > M.group (),  #  # # output # #  #   1 2 3 4  

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.

Importre P= Re.compile (r'(\w+) (\w+)') s='I say, hello world!' PrintP.sub (R'\2 \1', s)deffunc (m):returnM.group (1). Title () +' '+ M.group (2). Title ()PrintP.sub (func, s)## # output # # ##say I, World hello!#I Say, Hello world!

Subn (REPL, string[, Count]) |re.sub (pattern, REPL, string[, Count]):
Returns (Sub (REPL, string[, Count]), number of replacements).

Importre P= Re.compile (r'(\w+) (\w+)') s='I say, hello world!' PrintP.SUBN (R'\2 \1', s)deffunc (m):returnM.group (1). Title () +' '+ M.group (2). Title ()PrintP.subn (func, s)## # output # # ##(' Say I, World hello! ', 2)#(' I Say, Hello world! ', 2)

Python3 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.