[问题]求解一个python正则的跨行匹配的问题

2013-10-23 13:46:27 +08:00
 HDecember
http://v2.freep.cn/3tb_131023134156kqj9512293.jpg

urlContent1 获取不到匹配的内容,但是urlContent2可以 两个只差一个空行,
在正则上也加了跨行属性,为什么不能匹配呢?求解

——————以下是图片中的代码的文本部分——————

urlContent1 ='''

111
222
'''
urlContent2 ='''111
222
'''
global_pattern = re.compile('111',re.S|re.I|re.M)
globalMatch = global_pattern.match(urlContent1)
if globalMatch:
globalContent = globalMatch.group()
print 'urlContent1 match' + globalMatch.group()

globalMatch = global_pattern.match(urlContent2)
if globalMatch:
globalContent = globalMatch.group()
print 'urlContent2 match' + globalMatch.group()
5231 次点击
所在节点    Python
2 条回复
funagi
2013-10-23 13:59:01 +08:00
match()是从起始位置开始匹配,urlContent1起始位置是换行符,当然匹配不到了,应该用search()。
详见:http://wiki.ubuntu.org.cn/Python%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%93%8D%E4%BD%9C%E6%8C%87%E5%8D%97#match.28.29_vs_search.28.29
HDecember
2013-10-23 14:06:52 +08:00
喔喔~~~~好的~~~谢谢~~~!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/86607

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX