两个字符串之间的正则表达式匹配? [英] Regex matching between two strings?

查看:53
本文介绍了两个字符串之间的正则表达式匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法找到一种方法来提取所有评论,如下例所示.

<预><代码>>>>进口重新>>>字符串 = '''... <!-- 一个... -->... <!-- 两个 -- -- -->... <!-- 三-->...'''>>>m = re.findall ( '<!--([^\(-->)]+)-->', string, re.MULTILINE)>>>米['一\n','三']

带有 two -- -- 的块很可能由于错误的正则表达式而不匹配.有人可以指出我如何在两个字符串之间提取匹配项的正确方向.

<小时>

我已经测试了你们在评论中提出的建议......这里是几乎没有升级的工作解决方案.

<预><代码>>>>m = re.findall ( '', string, re.MULTILINE)>>>米[' 二三 ']>>>m = re.findall ( '<!--(.*\n?)-->', string, re.MULTILINE)>>>米['一\n','二---','三']

谢谢!

解决方案

这应该可以解决问题

 m = re.findall ( '', string, re.DOTALL)

I can't seem to find a way to extract all comments like in following example.

>>> import re
>>> string = '''
... <!-- one 
... -->
... <!-- two -- -- -->
... <!-- three -->
... '''
>>> m = re.findall ( '<!--([^\(-->)]+)-->', string, re.MULTILINE)
>>> m
[' one \n', ' three ']

block with two -- -- is not matched most likely because of bad regex. Can someone please point me in right direction how to extract matches between two strings.


Hi I've tested what you guys suggested in comments.... here is working solution with little upgrade.

>>> m = re.findall ( '<!--(.*?)-->', string, re.MULTILINE)
>>> m
[' two -- -- ', ' three ']
>>> m = re.findall ( '<!--(.*\n?)-->', string, re.MULTILINE)
>>> m
[' one \n', ' two -- -- ', ' three ']

thanks!

解决方案

this should do the trick

 m = re.findall ( '<!--(.*?)-->', string, re.DOTALL)

这篇关于两个字符串之间的正则表达式匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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