Both patterns and strings to be searched can be Unicode strings as well as 8-bit strings. 处理对象是unicode或者8位字符串与普通正则不同,python可以使用prefixed with'r' 即r‘XXXX 来代替反斜杠\ ,因为 "\\\\" 才能表示一个’\\‘ ,pattern string 大部分都 use Python’s raw string notation forregular expression patterns;
>>> print re.search(r'las.','dflast ini',re.S).group()#一个点匹配一个字符
last
>>> print re.search(r'las...','dflast ini',re.S).group()#3个点匹配3个字符,包含一个空格
last i
>>> print re.search(r'las.....','dflast \niop',re.S).group()#加了S可以匹配换行符\n
last
io
>>> print re.search(r'las.....','dflast \niop').group() #去掉标志位后出错
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
print re.search(r'las.....','dflast \niop').group()
AttributeError: 'NoneType' object has no attribute 'group'
>>>
为了看得直观,当然以上多个点可以用其它方式表示,后面讨论。
’^' ---匹配字符串的开始,在MULTILINE模式下,也匹配每一newline。
>>> print re.search(r'^last','lastert \nlastiop').group() #匹配字符串开始的第一个last
last
>>> print re.search(r'^last','llllastert\nlastiop',re.M).group() # M模式,匹配了第二行行首的last
last
>>> print re.search(r'^last','llllastert\nlastiop').group() #去掉M后匹配失败
Traceback (most recent call last):
File "<pyshell#37>", line 1, in <module>
print re.search(r'^last','llllastert\nlastiop').group()
AttributeError: 'NoneType' object has no attribute 'group'
{m,n}---- 匹配正则m到n次,a{3,5} will match from 3 to 5'a' characters。m缺省时表示0,n缺省时表示无限次。a{4,}b will matchaaaab or a thousand'a' characters followed by ab, but notaaab.中间的逗号不能省略。
‘\' ----转义字符,用来匹配 *,?等等。不是使用raw string的情况下,Python also uses the backslash as an escape sequence in string literals;也就是不使用r'XXX时,要使用两次反斜杠才能表示一个反斜杠,用raw简单些。
[ ]---存放字符集,1)中括号中的字符可以是单个的,e.g. [amk] will match'a','m', or'k'.
>>> print re.search('[amk]','sdafgfhmrtykyy').group()
a
>>> print re.search('[amk].','sdafgfhmrtykyy').group()
af
>>> print re.search('[mka].','sdafgfhmrtykyy').group()
af
是或关系,匹配了a,就没有再匹配 m, k。
2)也可以是一段有序字符集,for example[a-z] will match any lower case ASCII letter,[0-5][0-9] will match all the two-digits numbers from 00 to 59, and[0-9A-Fa-f] will match any hexadecimal digit. 如果'-'字符放在了字符类的开头处或末尾处,该字符就会被当作一个普通字符来看待 ,(e.g.[a\-z]) or if it’s placed as the first or last character (e.g.[a-]),it will match a literal'-'.
4)\w or\S也可以包含在其中,是否进行匹配主要取决于LOCALE or UNICODE mode 字符模式。
这个有单独用法
5)也可以进行反向匹配,first character of the set is'^', all the characters that are not in the set will be matched.[^5] will match any character except'5', and[^^] will match any character except'^'。
Traceback (most recent call last):
File "<pyshell#65>", line 1, in <module>
print re.search(r'(gra.)\w+(grr.)','sfdgrayfdfggrryfgg').group(3)
IndexError: no such group
>>> print re.findall(r'(gra.)\w+(grr.)','sfdgrayfdfggrryfgg')
[('gray', 'grry')]
编译标志让你可以修改正则表达式的一些运行方式。在 re 模块中标志可以使用两个名字,一个是全名如 IGNORECASE,一个是缩写,一字母形式如 I。(如果你熟悉 Perl 的模式修改,一字母形式使用同样的字母;例如 re.VERBOSE的缩写形式是 re.X。)多个标志可以通过按位 OR-ing 它们来指定。如 re.I | re.M 被设置成 I 和 M 标志:
这有个可用标志表,对每个标志后面都有详细的说明。
标志 含义
DOTALL, S 使 . 匹配包括换行在内的所有字符
IGNORECASE, I 使匹配对大小写不敏感
LOCALE, L 做本地化识别(locale-aware)匹配
MULTILINE, M 多行匹配,影响 ^ 和 $
VERBOSE, X 能够使用 REs 的 verbose 状态,使之被组织得更清晰易懂