正则表达式

正则表达式 regex

http://www.xuebuyuan.com/2042477.html

(+|?|*|{})?  非贪婪匹配

\d 数字    \w 数字和字母     \s 空格字符     \b单词边界border

(?#...)注释,可忽略

(?=...) Matches if ... matches next, but doesn't consume the string.               '(?=test)'   在hellotest中匹配hello

(?!...)Matches if ... doesn't match next.                                                '(?!=test)' 若hello后面不为test,匹配hello

(?<=...)Matches if preceded by ... (must be fixed length).

'(?<=hello)test'在hellotest中匹配test


flags:

re.I或者(?i)      忽略大小写  

        #  re.findall(r'(?i)yes','yes,YeS')      返回['yes', 'YeS']

re.M 或(?m)    多行混合 mix

re.S (. 可以表示\n)  

re.X (无视空白符和换行符,易于通过换行进行注释,使正则表达式更易读)

使用 r 前缀来表示正则(原生字符无转义) #eg:  s =r'ABC\-001'

re.match(,)  #从开头匹配

re.split(,)

m为正则对象

m.groups() #整个模式匹配结果 字符串形式返回

m.group(N) #返回第N个子组,N=0时返回整个模式匹配结果,等同于m.group()

match和search都只匹配一次,不同的是match从开始处匹配,search则从开始搜索。

findall() 返回一个列表,列表元素为字符串()

finditer() 返回迭代对象(节约内存)

finditer().next().groups()  #py2

next(finditer()).groups()  #py3

正则对象 n 编译后加快运行速度,n=re.compile(pattern)                          n.search('str').group()

sub('x','y','xxyyxy')    #'yyyyyy'

subn('x','y','xxyyxy')    #('yyyyyy',3) 返回一个二元元组,第二个参数为替换个数

re.sub(r'(\d{1,22})/(\d{1,2})/(\d{2}|\d{4})',r'\2/\1/\3','2/20/91')  #  \2表示第二个子组

你可能感兴趣的:(正则表达式)