python正则表达式findall和多行 [英] python regex findall and multiline

查看:69
本文介绍了python正则表达式findall和多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

蟒蛇 2.6.8

s= '''
foo 
bar
baz
'''
>>>re.findall(r'^\S*',s,re.MULTILINE)
['', 'foo', 'bar', 'baz', '']
>>>ptrn = re.compile(r'^\S*',re.MULTILINE)
>>>ptrn.findall(s)
['', 'foo', 'bar', 'baz', '']
>>>ptrn.findall(s,re.MULTILINE)
['baz', '']

为什么在 findall 中使用 MULTILINE 标志有区别?

Why is there a difference between using MULTILINE flag in findall?

推荐答案

在正则表达式对象上调用 findall() 方法时,第二个参数不是 flags参数(因为在编译正则表达式时已经使用过)但是 pos 参数,告诉正则表达式引擎在字符串中的哪个点开始匹配.

When calling the findall() method on a regex object, the second parameter is not the flags argument (because that has already been used when compiling the regex) but the pos argument, telling the regex engine at which point in the string to start matching.

re.MULTILINE 只是一个整数(恰好是 8).

re.MULTILINE is just an integer (that happens to be 8).

请参阅文档.

这篇关于python正则表达式findall和多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆