在RE匹配中重用一部分字符串? [英] reusing parts of a string in RE matches?

查看:70
本文介绍了在RE匹配中重用一部分字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能应该找到一个RE小组发帖,但我的新闻服务器在

工作似乎没有,所以我道歉。但这是用Python

无论如何:)


所以我的问题是,如何在
$ b $中找到所有出现的模式b字符串,包括重叠匹配?我认为它有事可做

有前瞻和后视,但我只有这么远:


import re

string =''abababababababab''

pattern = re.compile(r''ab(?= a)'')

m = pattern。 findall(string)


这匹配所有''ab''后跟''a'',但它不包括

''一个''。我想做的是找到所有''aba''匹配。一个常规的

findall()给出了四个结果,但实际上有七个。


有没有办法用RE模式做到这一点,或者我必须手动将'a''添加到比赛结尾?$ />

谢谢。

解决方案



John Salerno写道:

所以我的问题是,如何在一个模式中找到所有出现的模式
字符串,包括重叠匹配?我认为它有一些事情要做,而且有前瞻和后视,但我只是做到了这一点:

import re
string =''abababababababab' '
pattern = re.compile(r''ab(?= a)'')
m = pattern.findall(string)




为什么没有类似


导入的东西?b $ b string =''abababababababab''

pattern = re.compile(r" ^ aba")

ans = []
$ x $ b for x in xrange(len(string)):

m = pattern.match(string [ x:])

如果m:ans.append((x + m.start(),x + m.end()))


#现在ans是一对(p,q)的列表,其中子串字符串[p:q]

匹配模式


- Murali


John Salerno ???é??:

我可能应该找到一个要发布的RE组,但我的新闻服务器在
工作似乎没有,所以我道歉。但是无论如何这都是Python的


所以我的问题是,如何在
字符串中找到所有出现的模式,包括重叠匹配?我认为它有一些事情要做,而且有前瞻和后视,但我只是做到了这一点:

import re
string =''abababababababab' '
pattern = re.compile(r''ab(?= a)'')

以下来自python库参考


* |(?= ...)| *

匹配如果...匹配下一个,但不消耗任何字符串。

这称为前瞻断言。例如,Isaac(?= Asimov)

将匹配|''Isaac''|只有当它跟着'''Asimov''|。

m = pattern.findall(string)

这匹配所有''ab''后跟一个''a'',但它不包括
'''''。我想做的是找到所有''aba''匹配。一个常规的
findall()给出了四个结果,但实际上有七个。


我尝试了代码,但是我得到七个结果!有没有办法只使用RE模式,或者我必须手动将a添加到匹配结尾?

谢谢。



现在有人通常跳进来告诉你如何做这个

而不使用正则表达式并使用字符串方法代替。


我会看。


rd


I probably should find an RE group to post to, but my news server at
work doesn''t seem to have one, so I apologize. But this is in Python
anyway :)

So my question is, how can find all occurrences of a pattern in a
string, including overlapping matches? I figure it has something to do
with look-ahead and look-behind, but I''ve only gotten this far:

import re
string = ''abababababababab''
pattern = re.compile(r''ab(?=a)'')
m = pattern.findall(string)

This matches all the ''ab'' followed by an ''a'', but it doesn''t include the
''a''. What I''d like to do is find all the ''aba'' matches. A regular
findall() gives four results, but really there are seven.

Is there a way to do this with just an RE pattern, or would I have to
manually add the ''a'' to the end of the matches?

Thanks.

解决方案


John Salerno wrote:

So my question is, how can find all occurrences of a pattern in a
string, including overlapping matches? I figure it has something to do
with look-ahead and look-behind, but I''ve only gotten this far:

import re
string = ''abababababababab''
pattern = re.compile(r''ab(?=a)'')
m = pattern.findall(string)



Why not something like

import re
string = ''abababababababab''
pattern = re.compile(r"^aba")
ans = []
for x in xrange(len(string)):
m = pattern.match(string[x:])
if m: ans.append( (x+m.start(),x+m.end()))

# now ans is a list of pairs (p,q) where the substring string[p:q]
matches pattern

- Murali


John Salerno ???é??:

I probably should find an RE group to post to, but my news server at
work doesn''t seem to have one, so I apologize. But this is in Python
anyway :)

So my question is, how can find all occurrences of a pattern in a
string, including overlapping matches? I figure it has something to do
with look-ahead and look-behind, but I''ve only gotten this far:

import re
string = ''abababababababab''
pattern = re.compile(r''ab(?=a)'')
Below from the python library reference

*|(?=...)|*
Matches if ... matches next, but doesn''t consume any of the string.
This is called a lookahead assertion. For example, Isaac (?=Asimov)
will match |''Isaac ''| only if it''s followed by |''Asimov''|.
m = pattern.findall(string)

This matches all the ''ab'' followed by an ''a'', but it doesn''t include the
''a''. What I''d like to do is find all the ''aba'' matches. A regular
findall() gives four results, but really there are seven.

I try the code , but I give seven results ! Is there a way to do this with just an RE pattern, or would I have to
manually add the ''a'' to the end of the matches?

Thanks.




Right about now somebody usually jumps in and shows you how to do this
without using regex and using string methods instead.

I''ll watch.

rd


这篇关于在RE匹配中重用一部分字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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