python re模块

来源:互联网 发布:sql中replace的用法 编辑:程序博客网 时间:2024/05/29 09:39
Python re
    re库是Python里面负责正则表达(regular expression)的一个库。
    维基百科对正则表达的定义:
 In theoretical computer science and formal language theory, a regular expression (sometimes called a rational expression) is a sequence of characters that define a search pattern, mainly for use inpattern matching with strings, or string matching, i.e. "find and replace"-like operations.
 简而言之,正则表达式是一个字符串的搜索模式,本身也是一个字符串,而且语法和含义具有跨语言通用性,即大部分规定在很多语言里面都是相通的。 
 某种程度上可以把正则表达式理解成为一种语言,而组成这个语言最重要的就是元字符(
Metacharacter)了。下面是从re文档copy的元字符列表:
The special characters are:
        "."      Matches any character except a newline.
        "^"      Matches the start of the string.
        "$"      Matches the end of the string or just before the newline at
                 the end of the string.
        "*"      Matches 0 or more (greedy) repetitions of the preceding RE.
                 Greedy means that it will match as many repetitions as possible.
        "+"      Matches 1 or more (greedy) repetitions of the preceding RE.
        "?"      Matches 0 or 1 (greedy) of the preceding RE.
        *?,+?,?? Non-greedy versions of the previous three special characters.
        {m,n}    Matches from m to n repetitions of the preceding RE.
        {m,n}?   Non-greedy version of the above.
        "\\"     Either escapes special characters or signals a special sequence.
        []       Indicates a set of characters.
                 A "^" as the first character indicates a complementing set.
        "|"      A|B, creates an RE that will match either A or B.
        (...)    Matches the RE inside the parentheses.
                 The contents can be retrieved or matched later in the string.
        (?aiLmsux) Set the A, I, L, M, S, U, or X flag for the RE (see below).
        (?:...)  Non-grouping version of regular parentheses.
        (?P<name>...) The substring matched by the group is accessible by name.
        (?P=name)     Matches the text matched earlier by the group named name.
        (?#...)  A comment; ignored.
        (?=...)  Matches if ... matches next, but doesn't consume the string.
        (?!...)  Matches if ... doesn't match next.
        (?<=...) Matches if preceded by ... (must be fixed length).
        (?<!...) Matches if not preceded by ... (must be fixed length).
        (?(id/name)yes|no) Matches yes pattern if the group with id/name matched,
                           the (optional) no pattern otherwise.
    
    The special sequences consist of "\\" and a character from the list
    below.  If the ordinary character is not on the list, then the
    resulting RE will match the second character.
        \number  Matches the contents of the group of the same number.
        \A       Matches only at the start of the string.
        \Z       Matches only at the end of the string.
        \b       Matches the empty string, but only at the start or end of a word.
        \B       Matches the empty string, but not at the start or end of a word.
        \d       Matches any decimal digit; equivalent to the set [0-9] in
                 bytes patterns or string patterns with the ASCII flag.
                 In string patterns without the ASCII flag, it will match the whole
                 range of Unicode digits.
        \D       Matches any non-digit character; equivalent to [^\d].
        \s       Matches any whitespace character; equivalent to [ \t\n\r\f\v] in
                 bytes patterns or string patterns with the ASCII flag.
                 In string patterns without the ASCII flag, it will match the whole
                 range of Unicode whitespace characters.
        \S       Matches any non-whitespace character; equivalent to [^\s].
        \w       Matches any alphanumeric character; equivalent to [a-zA-Z0-9_]
                 in bytes patterns or string patterns with the ASCII flag.
                 In string patterns without the ASCII flag, it will match the
                 range of Unicode alphanumeric characters (letters plus digits
                 plus underscore).
                 With LOCALE, it will match the set [0-9_] plus characters defined
                 as letters for the current locale.
        \W       Matches the complement of \w.
        \\       Matches a literal backslash.

 正则表达的强大之处在于处理字符串的灵活性和简约性,一行可以顶的上N行常规匹配代码。
 举例:匹配email:\
w+([-+.]\w+)*@\w+([-.]\w+)*.\w+([-.]\w+)*
    \w匹配单词字符, 
([-+.]\w+)*表示可能含有以-或+或.开头后面跟若干字符,整个部分可能出现零至若干次。后面的类似。
    Python re库提供了几个函数,用来实现正则表达式的各种功能,包括查找、匹配、分割等等。文档同样提供了函数列表:
        match     Match a regular expression pattern to the beginning of a string.
        fullmatch Match a regular expression pattern to all of a string.
        search    Search a string for the presence of a pattern.
        sub       Substitute occurrences of a pattern found in a string.
        subn      Same as sub, but also return the number of substitutions made.
        split     Split a string by the occurrences of a pattern.
        findall   Find all occurrences of a pattern in a string.
        finditer  Return an iterator yielding a match object for each match.
        compile   Compile a pattern into a RegexObject.
        purge     Clear the regular expression cache.
        escape    Backslash all non-alphanumerics in a string.
     常用的主要有match,search,sub,split,findall。在介绍这些函数之前,先看看re库提供了哪些flags,即特殊匹配模式,比如可以指定忽略大小写,多行匹配,点符号能不能匹配新行等等:
Some of the functions in this module takes flags as optional parameters:
        A  ASCII       For string patterns, make \w, \W, \b, \B, \d, \D
                       match the corresponding ASCII character categories
                       (rather than the whole Unicode categories, which is the
                       default).
                       For bytes patterns, this flag is the only available
                       behaviour and needn't be specified.
        I  IGNORECASE  Perform case-insensitive matching.
        L  LOCALE      Make \w, \W, \b, \B, dependent on the current locale.
        M  MULTILINE   "^" matches the beginning of lines (after a newline)
                       as well as the string.
                       "$" matches the end of lines (before a newline) as well
                       as the end of the string.
        S  DOTALL      "." matches any character at all, including the newline.
        X  VERBOSE     Ignore whitespace and comments for nicer looking RE's.
        U  UNICODE     For compatibility only. Ignored for string patterns (it
                       is the default), and forbidden for bytes patterns.
 
    然后看几个函数:
match(pattern, string, flags=0)
        Try to apply the pattern at the start of the string, returning
        a match object, or None if no match was found.
    最基本的匹配的函数,接受三个参数,功能是从字符串开头开始匹配。也就是说,想要的pattern必须从字符串开头就符合,否则就算中间出现了,也不会认为是match。例如:
>>> text = "JGood is a handsome boy, he is cool, clever, and so on..."
>>> m = re.match(r"(\w+)\s", text)
>>> m
<_sre.SRE_Match object; span=(0, 6), match='JGood '>
>>> m.groups()
('JGood',)
>>> m.span()
(0, 6)  
>>> m = re.match(r"\s(\w+)", text)
>>> m       # m is none
 其中,pattern前面的r是raw的意思,用来避免转义,比如要匹配的是\n,假如没有r,输入的时候自动就当做换行处理了。当然这个例子加不加无所谓。
 返回的是一个matchObject,可以用groups()函数将match结果以tuple形式返回。同样还可以用span()函数得到match结果在原字符串的index范围。
 假如想要设置flags,在后面加上re.+匹配模式,例如
   
>>> m = re.match("jgood\s", text, re.I)
>>> m
<_sre.SRE_Match object; span=(0, 6), match='JGood '>
    
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.
    相当于整体版的match:
>>> m = re.search(r"\s(\w+)", text)
>>> m
<_sre.SRE_Match object; span=(5, 8), match=' is'>
     值得注意的是,search找到一个就返回了,如果想找到全部,用后面的findall。 

split(pattern, string, maxsplit=0, flags=0)
        Split the source string by the occurrences of the pattern,
        returning a list containing the resulting substrings.  If
        capturing parentheses are used in pattern, then the text of all
        groups in the pattern are 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 element
        of the list.
    分割函数,只不过是用正则表达式来分割。返回一个包含分割结果的list。
>>> m = re.split(r"\s", text)
>>> m
['JGood', 'is', 'a', 'handsome', 'boy,', 'he', 'is', 'cool,', 'clever,', 'and', 'so', 'on...']
 
sub(pattern, repl, string, count=0, flags=0)
        Return the string obtained by replacing the leftmost
        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 are processed.  If it is
        a callable, it's passed the match object and must return
        a replacement string to be used. 
    替代函数。将符合pattern的表达式用提供的字符串替换,还可以指定替换几个。
>>> m = re.sub(r"\s", "+", text, 3)
>>> m
'JGood+is+a+handsome boy, he is cool, clever, and so on...'

findall(pattern, string, flags=0)
        Return a list of all non-overlapping matches in the string.
        
        If one or more capturing groups are present in the pattern, return
        a list of groups; this will be a list of tuples if the pattern
        has more than one group.
        
        Empty matches are included in the result. 
    匹配全部能够匹配的,不像search找到一个就返回,而是搜索完毕整个字符串。
 
>>> m = re.findall(r"(\w+)\s", text)
>>> m
['JGood', 'is', 'a', 'handsome', 'he', 'is', 'and', 'so']
    总结:re库的作用,一般是在解决问题的时候对字符串进行处理,以方便后续运算。也有一些字符串处理题,re库可以直接解决,不过更多时候是充当一个中间工具。

例题:(翻转游戏)甲乙两个人玩一个翻转字符串的游戏,给出一个只有"+""-"符号的字符串,每个人每次可以把连续的“++”翻转成“--”,假如无法做到,则此人判负,游戏结束。由甲先开始。现在给出字符串s,返回甲是否有必胜的策略。
【解】这个题解法其实有不少,贴一个和re相关的。
首先要明白一个事情,那就是对于一个字符串,只有两种情况:甲必胜或者乙必胜。
证明(思路是:假设单个连续+序列必胜或必负的前提下,其组合必胜或必负;然后对于单个连续+序列,可以从简单情况开始推,前几个是一眼能看出来必胜和必负的。因此对于某个长度为i的,必定可以通过翻转++成为之前的两个连续+序列的组合,而之前的已经证明了,所以这一个也将会必胜或必负):
对于被-隔开的几个+序列pi,如果说1代表必胜,0代表必败的话,总体结果p=p0^p1...^pn-1. 
关于这个结论,只考虑两个连续的+序列a和b,无非三种情况:
(0,0),那么无论甲从哪一个开始,乙只要也在同一个序列里翻转即可,到一个序列完结,甲无法再翻转,因此在另一个序列甲还是先手必败。
(0,1),甲从必胜的开始,那么乙将被逼迫先手开必败,因此甲必胜。
(1,1),甲开始一个序列,但是乙总能先手开第二个必胜序列,因此最终乙必胜。
因此,总的结果就是两个分结果异或,推广到更多也是一样。
所以,对于单个连续的+序列,甲的选择就是相当于把一个裂开成为两个,这两个都比这个字符串小,因此之前就确定是必胜或者必负的。通过上面的证明可以知道,假如有某一个方法可以让裂开的两个子序列的组合是必败的,那么甲相反就是必胜的。否则甲必败。因此,对于单个连续+序列,也是必胜或必败,没有中间结果。
# Time:  O(n + c^2)
# Space: O(c)

import re

class SolutionM:
    
def canWin(selfs):
        lens = 
list(map(lenre.split('-+'s)))
        g = [
0]*(max(lens)+1)
        
for in range(1len(g)):
            subset = {x^y 
for xin zip(g[:i//2]reversed(g[i-1-i//2:i-1]))}
            
if subset:
                g[i] = 
min(subset)
            
else:
                g[i] = 
0
        
ret = 0
        
for in lens:
            ret ^= g[j]
        
return ret == 1
     这里的re.split('-+', s),里面的+可不是字符+,而是正则表达的元字符+,相当于说在有一个或者多个-的地方断开,即把连续+序列拿出来,类似于于re.findall('\+\++',s)【其实不完全等价,因为findall这个表达式只找两个及以上+符号的。不过实际上,一个单独的+可以忽略,和不存在没区别(和0异或不变),所以最后对结果没有影响。】。假如要想表达元字符的字符,需要加斜杠来escape。
    解法有点DP的味道,通过之前的结果来推下一个,g[i]表示长度为i的连续+序列的胜负,通过拆分判断其两个子序列的异或结果来判断这个字符串的胜负关系,然后利用上面证明的理论得到最终结果。
 
0 0