当前位置 : 首页 » 编程语言 » 正文

Python正则表达式非贪婪、多行匹配功能示例

分类 : 编程语言 | 发布时间 : 2018-04-21 09:06:21 | 浏览 : 0

本文实例讲述了Python正则表达式非贪婪、多行匹配功能。分享给大家供大家参考,具体如下:

一些regular的tips:

1 非贪婪flag

>>> re.findall(r"a(\d+?)","a23b") # 非贪婪模式
    ['2']
>>> re.findall(r"a(\d+)","a23b")
    ['23']

注意比较这种情况:

>>> re.findall(r"a(\d+)b","a23b")
    ['23']
>>> re.findall(r"a(\d+?)b","a23b") #如果前后均有限定条件,则非匹配模式失效
    ['23']

2 如果你要多行匹配,那么加上re.S和re.M标志

re.S:.将会匹配换行符,默认.逗号不会匹配换行符

>>> re.findall(r"a(\d+)b.+a(\d+)b","a23b\na34b")
    []
>>> re.findall(r"a(\d+)b.+a(\d+)b","a23b\na34b",re.S)
    [('23','34')]
>>>

re.M:^$标志将会匹配每一行,默认^只会匹配符合正则的第一行;默认$只会匹配符合正则的末行

>>> re.findall(r"^a(\d+)b","a23b\na34b")
    ['23']
>>> re.findall(r"^a(\d+)b","a23b\na34b",re.M)
    ['23','34']

但是,如果没有^标志,

>>> re.findall(r"a(\d+)b","a23b\na34b")
    ['23','43']

可见,是无需re.M

import re
n='''12 drummers drumming,
11 pipers piping, 10 lords a-leaping'''
p=re.compile('^\d+')
p_multi=re.compile('^\d+',re.MULTILINE) #设置 MULTILINE 标志
print re.findall(p,n) #['12']
print re.findall(p_multi,n) # ['12', '11']

import re
a = 'a23b'
print re.findall('a(\d+?)',a) #['2']
print re.findall('a(\d+)',a) #['23']
print re.findall(r'a(\d+)b',a) #['23']
print re.findall(r'a(\d+?)b',a) # ['23']

b='a23b\na34b'
''' . 匹配非换行符的任意一个字符'''
print re.findall(r'a(\d+)b.+a(\d+)b',b) #[]
print re.findall(r'a(\d+)b',b,re.M) # ['23', '34']
print re.findall(r'^a(\d+)b',b,re.M) # ['23', '34']
print re.findall(r'a(\d+)b',b) #['23','34'] 可以匹配多行
print re.findall(r'^a(\d+)b',b) # ['23'] 默认^只会匹配符合正则的第一行
print re.findall(r'a(\d+)b$',b) # ['34'] 默认$只会匹配符合正则的末行
print re.findall(r'a(\d+)b',b,re.M) #['23', '34']
print re.findall(r'a(\d+)b.?',b,re.M) # ['23', '34'] 表达式中的'.'匹配除换行符以外的字符,'?'匹配前一个字符0次或1次
print re.findall(r"a(\d+)b", "a23b\na34b") # ['23', '34']

注:Python3.4中,print为函数,需要加括号

PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript

正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg

更多关于Python相关内容可查看本站专题:《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关阅读:

raw_input function in Python

How to import the class within the same directory or sub directory?

How to return dictionary keys as a list in Python?

Python `if x is not None` or `if not x is None`?

Assign output of os.system to a variable and prevent it from being displayed on the screen

How can I reverse a list in Python?

What are the differences between type() and isinstance()?

How to install packages using pip according to the requirements.txt file from a local directory?

How to lowercase a string in Python?

Correct way to write line to file?