如何获得重叠选项模式的最长匹配?

发布于 2025-02-12 15:42:42 字数 333 浏览 0 评论 0原文

我正在处理Python正则表达式,我试图获得最长的模式匹配,其中包括重叠选项。

考虑此示例:

import re
task = "s290_fpga_simv_test_verilog"
pattern_str = "(s290|s290_fpga|s289|s289_fpga|s274|s274_fpga)"
result = re.match(pattern_str, task)
print(result.group(1))

它为我提供了输出s290,我期望s290_fpga。获得最长可能的匹配需要什么?

I am dealing with Python regular expressions where I am trying to get the longest match of a pattern that includes overlapping options.

Consider this example:

import re
task = "s290_fpga_simv_test_verilog"
pattern_str = "(s290|s290_fpga|s289|s289_fpga|s274|s274_fpga)"
result = re.match(pattern_str, task)
print(result.group(1))

It gives me the output s290 where I am expecting the longer s290_fpga. What is necessary to get the longest possible match?

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

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

发布评论

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

评论(1

新一帅帅 2025-02-19 15:42:42

扭转您的比赛顺序,因此您在正确时会变得不那么具体。您的代码是正确的,但是re.match()s290上找到匹配,然后停止。如果您想要结果s290_fpga将您的订单交换为:

"(s290_fpga|s290 etc...)"

Reverse your order of matches so you become less specific as you go to right. Your code is correct but the re.match() finds a match at s290 and then stops. If you want the result s290_fpgaswap your order to:

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