再次计算重叠的正则表达式匹配
如何使用 Python 获取重叠正则表达式匹配的数量?
我已阅读并尝试了 这个, 那个和其他一些问题,但发现没有一个适合我的场景。这里是:
- 输入示例字符串:
akka
- 搜索模式:
a.*k
正确的函数应该产生 2 作为匹配数,因为有两个可能的结束位置(k
字母)。
模式也可能更复杂,例如 a.*k.*a
也应该在 akka
中匹配两次(因为有两个 k
> 在中间)。
How can I obtain the number of overlapping regex matches using Python?
I've read and tried the suggestions from this, that and a few other questions, but found none that would work for my scenario. Here it is:
- input example string:
akka
- search pattern:
a.*k
A proper function should yield 2 as the number of matches, since there are two possible end positions (k
letters).
The pattern might also be more complicated, for example a.*k.*a
should also be matched twice in akka
(since there are two k
's in the middle).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您正在寻找的内容可能最好使用像 lepl 这样的解析库来完成:
我相信
parser.parse_all
的输出长度就是您要查找的内容。请注意,如果您的模式与整个字符串不匹配,您需要使用
parser.config.no_full_first_match()
来避免错误。编辑:根据@Shamanu4的评论,我看到你想要从任何位置开始匹配结果,你可以这样做:
I think that what you're looking for is probably better done with a parsing library like lepl:
I believe that the length of the output from
parser.parse_all
is what you're looking for.Note that you need to use
parser.config.no_full_first_match()
to avoid errors if your pattern doesn't match the whole string.Edit: Based on the comment from @Shamanu4, I see you want matching results starting from any position, you can do that as follows:
是的,它很丑陋且未经优化,但它似乎正在工作。这是对所有可能但独特变体
测试的简单尝试:
Yes, it is ugly and unoptimized but it seems to be working. This is a simple try of all possible but unique variants
test: