Python 正则表达式
来源:互联网 发布:win7激活 知乎 编辑:程序博客网 时间:2024/06/06 00:10
1、常见正则表达式符号和特殊字符
2、常见正则表达式属性
3、re模块函数的使用
match() 方法匹配字符串
# 需要导入 re 模块>>> import re>>> m = re.match('fo', 'foo')>>> m<_sre.SRE_Match object; span=(0, 2), match='fo'>>>> m.group()'fo'# 如果匹配失败, 将会抛出 AttributeError 异常# 可以用 if m is not None: m.group() 先判断是否为 None>>> m = re.match('fo', 'ofo ofo ')>>> m>>> m.group()Traceback (most recent call last): File "<pyshell#8>", line 1, in <module> m.group()AttributeError: 'NoneType' object has no attribute 'group'# 模式匹配只能从字符串的起始部分开始, 即使字符串更长,匹配也能成功>>> m = re.match('fo', 'fo 0000, lll')>>> m.group()'fo'
search() 方法匹配字符串
# search 函数会搜索在字符串中第一次出现的位置,并且从左向右搜索>>> m = re.search('fo', 'we foo iiie foo')>>> m<_sre.SRE_Match object; span=(3, 5), match='fo'>
匹配多个字符
>>> bt = 'bat|bct|bbt'>>> s = re.search(bt, 'foo bat bct')>>> s.group()'bat'
[]字符集
>>> m = re.match('[cr][23][dp][o2]', 'c3po')>>> m.group()'c3po'
用groups() 子组获得一个包含所有匹配子组的元组
>>> m = re.match('\w\w\w-\d\d\d', 'abc-123')>>> m.group()'abc-123'>>> m.group(1)Traceback (most recent call last): File "<pyshell#49>", line 1, in <module> m.group(1)IndexError: no such group>>> m.group(0)'abc-123'>>> m = re.match('(\w\w\w)-(\d\d\d)', 'abc-123')# m.group() 和 m.group(0) 的效果是一样的 >>> m.group()'abc-123'>>> m.group(1)'abc'>>> m.group(0)'abc-123'>>> m.group(2)'123'>>> m = re.match('ab', 'ab')>>> m.group()'ab'>>> m.groups()()>>> >>> m = re.match('(ab)', 'ab')>>> m.group()'ab'>>> m.group(1)'ab'>>> m.group(0)'ab'>>> m.group(2)Traceback (most recent call last): File "<pyshell#64>", line 1, in <module> m.group(2)IndexError: no such group>>> m.groups()('ab',)>>> m = re.match('(a)(b)', 'ab')>>> m.group()'ab'>>> m.group(1)'a'>>> m.group(0)'ab'>>> m.group(2)'b'>>> m.groups()('a', 'b')# groups() 是根据 pattern 由外到内分组的 >>> m = re.match('(a(b))', 'ab')>>> m.group()'ab'>>> m.group(0)'ab'>>> m.group(1)'ab'>>> m.group(2)'b'>>> m.groups()('ab', 'b')>>> m = re.match('(a(b(c)))', 'abc')>>> m.group()'abc'>>> m.group(0)'abc'>>> m.group(1)'abc'>>> m.group(2)'bc'>>> m.group(3)'c'>>> m.groups()('abc', 'bc', 'c')>>> m = re.match('a(b(c(d)))', 'abcd')>>> m.group()'abcd'>>> m.group(0)'abcd'>>> m.group(1)'bcd'>>> m.group(2)'cd'>>> m.groups()('bcd', 'cd', 'd')
匹配字符串的起始和结尾以及单词边界
# ^ 用来匹配以 pattern 作为起始的字符串>>> m = re.match('^from', 'from')>>> m.group()'from'>>> m = re.match('^from','fromasdads')>>> m.group()'from'>>> m = re.match('^from', 'd from')>>> m.group()Traceback (most recent call last): File "<pyshell#129>", line 1, in <module> m.group()AttributeError: 'NoneType' object has no attribute 'group'# $ 用来匹配以 pattern 作为结尾的字符串, 测试了很多都是报错, 不知# 道什么鬼>>> m = re.match('/bin/tcsh$', '/bin/tcsh')>>> m.group()'/bin/tcsh'>>> m = re.match('ad$', 'ad adadad')>>> m.group()Traceback (most recent call last): File "<pyshell#122>", line 1, in <module> m.group()AttributeError: 'NoneType' object has no attribute 'group'>>> m = re.group('ad$', 'ad ad ad')Traceback (most recent call last): File "<pyshell#123>", line 1, in <module> m = re.group('ad$', 'ad ad ad')AttributeError: module 're' has no attribute 'group'>>> m = re.match('ad$', 'ad ad ad')>>> m.group()Traceback (most recent call last): File "<pyshell#125>", line 1, in <module> m.group()AttributeError: 'NoneType' object has no attribute 'group'>>> m = re.match('bin$', 'aa bin')>>> m.group()Traceback (most recent call last): File "<pyshell#131>", line 1, in <module> m.group()AttributeError: 'NoneType' object has no attribute 'group'# 后来才发现要用search() 函数>>> s = re.search('/bin/bac$', 'dada/bin/bac')>>> s.group()'/bin/bac'# ^ 和 $ 组合,pattern 匹配单独构成的字符串>>> m = re.match('^bin$', 'bin')>>> m.group()'bin'>>> m = re.match('^bin$', 'bin bin')>>> m.group()Traceback (most recent call last): File "<pyshell#138>", line 1, in <module> m.group()AttributeError: 'NoneType' object has no attribute 'group'>>> m.group()'the'# \b 匹配边界 \B 匹配不以 pattern 为边界的字符串>>> m = re.match(r'\bhte', 'thr the thr')>>> m.group()Traceback (most recent call last): File "<pyshell#151>", line 1, in <module> m.group()AttributeError: 'NoneType' object has no attribute 'group'>>> m = re.match(r'\Bthe', 'thr the thr')>>> m.group()Traceback (most recent call last): File "<pyshell#153>", line 1, in <module> m.group()AttributeError: 'NoneType' object has no attribute 'group'>>> m = re.match(r'\Bthe', 'thenthethe')>>> m.group()Traceback (most recent call last): File "<pyshell#155>", line 1, in <module> m.group()AttributeError: 'NoneType' object has no attribute 'group'>>> m = re.group(r'\Bthe', 'thithe')Traceback (most recent call last): File "<pyshell#156>", line 1, in <module> m = re.group(r'\Bthe', 'thithe')AttributeError: module 're' has no attribute 'group'>>> m = re.match('r\Bthe', 'thithe')>>> m.group()Traceback (most recent call last): File "<pyshell#158>", line 1, in <module> m.group()AttributeError: 'NoneType' object has no attribute 'group'>>> m = re.match(r'\Bthe', 'banbinthe')>>> m.group()Traceback (most recent call last): File "<pyshell#161>", line 1, in <module> m.group()AttributeError: 'NoneType' object has no attribute 'group'>>> m = re.match(r'\Bthe', 'the theathe')>>> s = re.search(r'\Bthe', 'the thathe')>>> s.group()'the'>>> s = re.search('/bin/bac$', 'dada/bin/bac')>>> s.group()'/bin/bac'>>> m = re.match(r'\Bthe', 'the theathe')>>> s = re.search(r'\Bthe', 'the thathe')>>> s.group()'the'>>> s = re.search(r'\bthe', 'thr the tha')>>> s.group()'the'
findall() 与 finditer()
# findall() 查询字符串中符合 pattern 的全部非重复出现情况,返回列表>>> re.findall('car', 'carry the barcardi to the car')['car', 'car', 'car']# finditer() 比 findall() 节省内存并在匹配对象中迭代>>> for i in re.finditer(r'\d+', '2 2663 887 322'): print(i.group()) 22663887322
sub() 和 subn()
# subn() 和 sub() 几乎一样, subn() 还可以返回一个表示替换的总数>>> re.sub('[abc]', '789', 'aadhkahdkankdkdnlanl')'789789dhk789hdk789nkdkdnl789nl'>>> re.subn('[abc]', '789', 'adadadaddq3 euduashbfiau')('789d789d789d789ddq3 eudu789sh789fi789u', 7)
split() 分隔字符串
>>> re.split(':', 'str1:str2:str3')['str1', 'str2', 'str3']
0 0
- python正则表达式学习
- c++正则表达式,python
- Python正则表达式(一)
- python的正则表达式
- Python正则表达式
- Python初学-正则表达式
- 正则表达式与python
- python正则表达式学习
- python正则表达式学习
- python正则表达式学习
- python正则表达式
- Python - 正则表达式
- Python正则表达式
- python正则表达式
- Python正则表达式指南
- Python正则表达式指南
- Python 正则表达式指南
- python 正则表达式
- mysql查看变量设置
- CodeForces 731 F.Video Cards(水~)
- 爱摘苹果的小明
- python连续赋值可能引发的错误
- Linux(4)-压缩和解压缩命令
- Python 正则表达式
- 决策树的简单实现
- A
- 例题8-1 UVA
- xdoj 1190: 虢莔薅参加运动会
- 0欧姆电阻的作用
- 刷题-CC150-Java实现
- Codeforces gym 101102 D 单调栈
- POJ2080简单模拟