如何获得重叠选项模式的最长匹配?
我正在处理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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
扭转您的比赛顺序,因此您在正确时会变得不那么具体。您的代码是正确的,但是
re.match()
在s290
上找到匹配,然后停止。如果您想要结果s290_fpga
将您的订单交换为: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 ats290
and then stops. If you want the results290_fpga
swap your order to: