mav*_*ili 13 python regex multiple-matches
我正在尝试将模式与可能具有多个模式实例的字符串进行匹配.我需要单独的每个实例.re.findall() 应该这样做,但我不知道我做错了什么.
pattern = re.compile('/review: (http://url.com/(\d+)\s?)+/', re.IGNORECASE)
match = pattern.findall('this is the message. review: http://url.com/123 http://url.com/456')
Run Code Online (Sandbox Code Playgroud)
我需要" http://url.com/123 ",http://url.com/456和两个数123 456是不同的元素match列表.
我也尝试'/review: ((http://url.com/(\d+)\s?)+)/'过这种模式,但没有运气.
Nar*_*ala 15
Use this. You need to place 'review' outside the capturing group to achieve the desired result.
pattern = re.compile(r'(?:review: )?(http://url.com/(\d+))\s?', re.IGNORECASE)
Run Code Online (Sandbox Code Playgroud)
This gives output
>>> match = pattern.findall('this is the message. review: http://url.com/123 http://url.com/456')
>>> match
[('http://url.com/123', '123'), ('http://url.com/456', '456')]
Run Code Online (Sandbox Code Playgroud)
你在正则表达式中有额外的东西.在python中,模式应该只是一个字符串.例如,而不是这样:
pattern = re.compile('/review: (http://url.com/(\d+)\s?)+/', re.IGNORECASE)
Run Code Online (Sandbox Code Playgroud)
它应该是:
pattern = re.compile('review: (http://url.com/(\d+)\s?)+', re.IGNORECASE)
Run Code Online (Sandbox Code Playgroud)
通常在python中你实际上使用这样的"原始"字符串:
pattern = re.compile(r'review: (http://url.com/(\d+)\s?)+', re.IGNORECASE)
Run Code Online (Sandbox Code Playgroud)
字符串前面的额外r可以避免你不得不做大量的反斜杠转义等.
| 归档时间: |
|
| 查看次数: |
26188 次 |
| 最近记录: |