正则表达式从右方向匹配

发布于 2024-09-27 11:28:08 字数 216 浏览 1 评论 0原文

通常,我们使用正则表达式从左到右方向匹配。我想知道python中是否有一些开关可以用来从右到左匹配? 或者这个功能是否嵌入到任何其他语言中?

例如,

abcd1_abcd2

如果给定正则表达式abcd,它将匹配两个abcd 字符串。我想要的是将最后一场比赛放在第一位,从而反向匹配。

Normally, we use regular expression match from left to right direction. I want to know whether there is some switch that can be used to match from the right to left in python?
Or is this feature embedded in any other language?

e.g.

abcd1_abcd2

If given regular expression abcd, it will match two abcd strings. What I want is to put the last match at first, thus matching in reverse direction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮光沉寂 2024-10-04 11:28:08

您可以按照@SilentGhost 的建议反转列表:

import re

for s in reversed(re.findall('abcd.', 'abcd1_abcd2')):
    print s

You can reverse the list as proposed by @SilentGhost:

import re

for s in reversed(re.findall('abcd.', 'abcd1_abcd2')):
    print s
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文