对于一些预约义的字符集能够使用转义码能够更加紧凑的表示,re能够识别的转义码有3对,6个,分别为三个字母的大小写,他们的意义是相反的。html
\d : 一个数字
\D : 一个非数字
\w : 字母或者数字
\W : 非字母,非数字
\s : 空白符(制表符,空格,换行符等)
\S : 非空白符python
若是想指定匹配的内容在文本的相对位置,能够使用锚定,跟转义码相似。正则表达式
^ 字符或行的开始
$ 字符或行的结束
\A 字符串的开始
\Z 字符串结束
\b 一个单词开头或者末尾的空串
\B 不在一个单词开头或末尾的空串函数
import re the_str = "This is some text -- with punctuation" re.search(r'^\w+', the_str).group(0) # This re.search(r'\A\w+', the_str).group(0) # This re.search(r'\w+\S*$', the_str).group(0) # punctuation re.search(r'\w+\S*\Z', the_str).group(0) # punctuation re.search(r'\w*t\W*', the_str).group(0) # text -- re.search(r'\bt\w+', the_str).group(0) # text re.search(r'\Bt*\B', the_str).group(0) # 没有匹配
用组来解析匹配,简单的说就是在一个正则表达式中有几个小括号()将匹配的表达式分红不一样的组,使用group()函数来获取某个组的匹配,其中0为整个正则表达式所匹配的内容,后面从1开始从左往右依次获取每一个组的匹配,即每一个小括号中的匹配。使用groups()能够获取全部的匹配内容。this
import re the_str = "--aabb123bbaa" pattern = r'(\W+)([a-z]+)(\d+)(\D+)' match = re.search(pattern, the_str) match.groups() # ('--', 'aabb', '123', 'bbaa') match.group(0) # '--aabb123bbaa' match.group(1) # '--' match.group(2) # 'aabb' match.group(3) # '123' match.group(4) # 'bbaa'
python对分组的语法作了扩展,咱们能够对每一个分组进行命名,这样即可以使用名称来调用。语法:(?P<name>pattern)
,使用groupdict()能够返回一个包含了组名的字典。code
import re the_str = "--aabb123bbaa" pattern = r'(?P<not_al_and_num>\W+)(?P<al>[a-z]+)(?P<num>\d+)(?P<not_num>\D+)' match = re.search(pattern, the_str) match.groups() # ('--', 'aabb', '123', 'bbaa') match.groupdict() # {'not_al_and_num': '--', 'not_num': 'bbaa', 'num': '123', 'al': 'aabb'} match.group(0) # '--aabb123bbaa' match.group(1) # '--' match.group(2) # 'aabb' match.group(3) # '123' match.group(4) # 'bbaa' match.group('not_al_and_num') # '--' match.group('al') # 'aabb' match.group('num') # '123' ' match.group('not_num') # 'bbaa'
以上的group()方法在使用的时候须要注意,只有在有匹配的时候才会正常运行,不然会抛错,因此在不能保证有匹配而又要输出匹配结果的时候,必须作校验。htm
在re中能够设置不通的标志,也就是search()和compile()等中都包含的缺省变量flag。使用标志能够进行完成一些特殊的要求,如忽略大小写,多行搜索等。文档
import re the_str = "this Text" re.findall(r'\bt\w+', the_str) # ['this'] re.findall(r'\bt\w+', the_str, re.IGNORECASE) # ['this', 'Text']
关于搜索选项有不少,具体可查看文档 http://docs.python.org/2/library/re.html#module-re字符串