Python and Regular Expressions: RE module details

Source: Internet
Author: User

The RE module is a module that handles expressions in Python

Regular Expression Knowledge Reserve: http://www.cnblogs.com/huamingao/p/6031411.html

1. Match (pattern, string, flags=0)

Matches from the beginning of the string, the match returns a matching object, and the match fails to return none

Several values of flags
X Ignore spaces and comments

I ignore the difference between case case-insensitive matching

S. Match any character, including new lines

def match (pattern, string, flags=0): "" "    Try to apply thepattern at the start of the string, RETURNING
        a Match object, or None if no match was found. return _compile (pattern, flags). Match (String)   

2. Search (Pattern, string, flags=0)

Browse the entire string to match the first one, the unmatched successful return none

def search (pattern, string, flags=0): "" "    Scan through string looking for a match to the pattern, returning    a Match object, or None if no match was found. return _compile (pattern, flags). Search (String)   

3. FindAll (Pattern, string, flags=0)

Both match and search are used to match single values, that is, only one of the strings can be matched, and if you want to match all eligible elements in a string, you need to use FindAll.

findall,获取非重复的匹配列表;如果有一个组则以列表形式返回,且每一个匹配均是字符串;如果模型中有多个组,则以列表形式返回,且每一个匹配均是元祖;空的匹配也会包含在结果中

def findall (pattern, string, flags=0): "" "    Return A list of all non-overlapping matches in the string.    If one or more capturing groups is present in the pattern, return    a list of groups; This would be a list of tuples if The pattern has more    than one group.    Empty matches is included in the result. return _compile (pattern, flags). FindAll (String)   

4. Sub (pattern,repl,string,count=0,flags=0)

替换匹配成功的指定位置字符串

def sub (pattern, REPL, String, count=0, flags=0): "" "    Return the string obtained by replacing the leftmost< C6/>non-overlapping occurrences of the pattern in string by the    replacement repl.  REPL can be either a string or a callable;    If a string, backslash escapes in it is processed.  If It    is a callable, it's passed the match object and must return    a replacement string to be used.  return _compile (pattern, flags). Sub (repl, String, count)   
 5. Split (pattern,string,maxsplit=0,flags=0) 根据正则匹配分割字符串
def split (pattern, String, maxsplit=0, flags=0): "" "    split the source string by the occurrences of the Patt Ern,    returning a list containing the resulting substrings.  If    capturing parentheses is used in pattern and then the text of all    groups in the pattern is also returned as part of the resulting    list.  If Maxsplit is nonzero, at most maxsplit splits occur, and the remainder of the string is returned as the    final Eleme NT of the    list.  return _compile (pattern, flags). Split (String, maxsplit)   
 

6.compile ()

The Python code will eventually be compiled into bytecode before being executed by the interpreter. Before pattern matching, the expression pattern must first be compiled into a Regex object, pre-compilation can improve performance, re.compile () is used to provide this functionality.

7. Group () and groups ()

Two main ways to match objects:

Group () Returns all matching objects, or returns a specific subgroup, and returns all matching objects if there are no subgroups

Groups () returns a tuple that contains a unique or all child group, and returns an empty tuple if there are no subgroups

    Def group (self, *args): "" "Return one or more subgroups of the        match.        : rtype:t | Tuple        def groups (self, default= "     " "Pass
Original address: http://www.cnblogs.com/huamingao/p/6062367.html

Python and Regular Expressions: Re module detailed

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.