正则表达式
小胖最近在找工作,但是找工作的这个地方并不是很好找,所以要好好复习一番。
第一个复习的就是正则表达式。
正则表达式在线测试网站
http://tool.oschina.net/regex
匹配语法
不可见字符 | 描述 |
---|---|
\f | 匹配一个换页符。 |
\n | 匹配一个换行符。 |
\r | 匹配一个回车符。 |
\s | 匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。注意 Unicode 正则表达式会匹配全角空格符。 |
\S | 匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。 |
\t | 匹配一个制表符 |
\v | 匹配一个垂直制表符 |
字符 | 描述 |
---|---|
$ | 匹配输入字符串的结尾位置。如果设置了 RegExp 对象的 Multiline 属性,则 $ 也匹配 ‘\n’ 或 ‘\r’。要匹配 $ 字符本身,请使用 \$。 |
( ) | 标记一个子表达式的开始和结束位置。子表达式可以获取供以后使用。要匹配这些字符,请使用 \( 和 \)。 |
* | 匹配前面的子表达式零次或多次。要匹配 * 字符,请使用 \*。 |
+ | 匹配前面的子表达式一次或多次。要匹配 + 字符,请使用 \+。 |
. | 匹配除换行符 \n 之外的任何单字符。要匹配 . ,请使用 \. 。 |
[ | 标记一个中括号表达式的开始。要匹配 [,请使用 \[。 |
? | 匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。要匹配 ? 字符,请使用 \?。 |
\ | 将下一个字符标记为或特殊字符、或原义字符、或向后引用、或八进制转义符。例如, ‘n’ 匹配字符 ‘n’。‘\n’ 匹配换行符。序列 ‘\\’ 匹配 “\”,而 ‘\(’ 则匹配 “(”。 |
^ | 匹配输入字符串的开始位置,除非在方括号表达式中使用,此时它表示不接受该字符集合。要匹配 ^ 字符本身,请使用 \^。 |
{ | 标记限定符表达式的开始。要匹配 {,请使用 \{。 |
| | 指明两项之间的一个选择。要匹配 |
限定符用来指定正则表达式的一个给定组件必须要出现多少次才能满足匹配。
限定符 | 描述 |
---|
- | 匹配前面的子表达式零次或多次。例如,zo* 能匹配 “z” 以及 “zoo”。* 等价于{0,}。
+| 匹配前面的子表达式一次或多次。例如,‘zo+’ 能匹配 “zo” 以及 “zoo”,但不能匹配 “z”。+ 等价于 {1,}。
? | 匹配前面的子表达式零次或一次。例如,“do(es)?” 可以匹配 “do” 、 “does” 中的 “does” 、 “doxy” 中的 “do” 。? 等价于 {0,1}。
{n} | n 是一个非负整数。匹配确定的 n 次。例如,‘o{2}’ 不能匹配 “Bob” 中的 ‘o’,但是能匹配 “food” 中的两个 o。
{n,} | n 是一个非负整数。至少匹配n 次。例如,‘o{2,}’ 不能匹配 “Bob” 中的 ‘o’,但能匹配 “foooood” 中的所有 o。‘o{1,}’ 等价于 ‘o+’。‘o{0,}’ 则等价于 ‘o*’。
{n,m}| m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。例如,“o{1,3}” 将匹配 “fooooood” 中的前三个 o。‘o{0,1}’ 等价于 ‘o?’。请注意在逗号和两个数之间不能有空格。
匹配优先级:从左到右 下面是优先级从高到低
字符 | 描述 |
---|---|
\ | 转义符 |
(), (?: ), (?=), [] | 圆括号和方括号 |
*, +, ?, {n}, {n,}, {n,m} | 限定符 |
^, $, \ | 任何元字符、任何字符 定位点和序列(即:位置和顺序) |
| | 替换,"或"操作 |
python 正则表达式
group和groups
先说一下group和groups的区别:
group(0)匹配查询结果 group(1)-group(最后一个) 匹配单个查询结果
groups()匹配group(1)-group(最后一个)
eg:
import re
s = 'a123456789asfjoink987654321'
m = '([a-z]*)([0-9]*)([a-z]*)([0-9]*)'
r = re.compile(m, re.I).search(s)
for i in range(r.lastindex):
print r.group(i)
print r.groups()
结果:
a123456789asfjoink987654321
a
123456789
asfjoink
(‘a’, ‘123456789’, ‘asfjoink’, ‘987654321’)
python中正则表达式模块为re 我们来看看re都有哪些功能吧~
import re
print dir(re)
结果:
[‘DEBUG’, ‘DOTALL’, ‘I’, ‘IGNORECASE’, ‘L’, ‘LOCALE’, ‘M’, ‘MULTILINE’, ‘S’, ‘Scanner’, ‘T’, ‘TEMPLATE’, ‘U’, ‘UNICODE’, ‘VERBOSE’, ‘X’, ‘MAXCACHE’,
‘__all__’, ‘__builtins__’, ‘__doc__’, '_file__’, ‘__name__’, ‘__package__’, ‘__version__’, ‘_alphanum’, ‘_cache’, ‘_cache_repl’, ‘_compile’, ‘_compile_repl’, ‘_expand’, ‘_locale’, ‘_pattern_type’, ‘_pickle’, ‘_subx’,
‘compile’, ‘copy_reg’, ‘error’, ‘escape’, ‘findall’, ‘finditer’, ‘match’, ‘purge’, ‘search’, ‘split’, ‘sre_compile’, ‘sre_parse’, ‘sub’, ‘subn’, ‘sys’, ‘template’]
下面我们来使用常用re所包含的方法了解其用法
compile
python 对 compile的解释是Compile a regular expression pattern, returning a pattern object.-> 编译一个正则表达式模式,返回一个模式对象。
这里面的模式为上述常量:
常量 | 常量全称 | 常量来源 | 注释 |
---|---|---|---|
I | IGNORECASE | sre_compile.SRE_FLAG_IGNORECASE | ignore case:忽略大小写 |
L | LOCALE | sre_compile.SRE_FLAG_LOCALE | assume current 8-bit locale:假设当前是8位 |
U | UNICODE | sre_compile.SRE_FLAG_UNICODE | assume unicode locale :假设当前是Unicode编码 |
M | MULTILINE | sre_compile.SRE_FLAG_MULTILINE | make anchors look for newline:让集合寻找换行 |
S | DOTALL | sre_compile.SRE_FLAG_DOTALL | make dot match newline:让点匹配换行 |
X | VERBOSE | sre_compile.SRE_FLAG_VERBOSE | ignore whitespace and comments:忽略空格和注释 |
import re
s = 'a123456789asfjoink987654321'
m = '[0-9]+'
r = re.compile(m, re.I).search(s)
# search:Scan through string looking for a match, and return a corresponding->扫描字符串寻找匹配结果 返回相应值
match instance. Return None if no position in the string matches
print r.group()
r = re.compile(m, re.I).match(s)
# 这边match是从第一个字符开始比较,查找匹配到的结果, 如果开头不匹配,则结果为None match:Matches zero | more characters at the beginning of the string->匹配第0-多位字符串开头
print r.group()
findall
python 对findall的解释是
'''Return a list of all non-overlapping matches in the string.
If one or more 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.'''
返回字符串中所有非重叠匹配项的列表。
如果模式中存在一个或多个组,则返回组列表;如果模式有多个组,这将是一个元组列表。结果中包含空匹配.
举例:
import re
s = 'a123456789asfjoink987654321'
m = '[a-z]+'
r = re.findall(m, s)
print r # ['a', 'asfjoink']
finditer
''' Return an iterator over all non-overlapping matches in the
string. For each match, the iterator returns a match object.
Empty matches are included in the result.'''
在字符串中所有非重叠匹配项上返回一个迭代器。对于每个匹配,迭代器返回一个匹配对象。
结果中包含空匹配
举例:
import re
s = 'a123456789asfjoink987654321'
m = '[a-z]+'
r = re.finditer(m, s)
for i in r:
print i.group()
结果:
a
asfjoink
match与search
match
'''Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found.'''
尝试在字符串的开头应用模式,返回匹配对象,如果没有找到匹配,则返回None
search
Scan through string looking for a match to the pattern, returning
a match object, or None if no match was found."
扫描字符串寻找匹配结果 返回相应值或者None
import re
s = '123456789asfjoink987654321'
m = '[a-z]+'
r = re.match(m, s)
print r.group() #a 如果字符串开头不为a则返回None
r = re.search(m, s)
print r.group() #a 如果字符串开头去掉a 则返回asfjoink