[英]python regex for conditional (|) matching
Looking for a python regex pattern. 寻找python正则表达式模式。 Seems like it has to exist, but it has me stumped.
似乎它必须存在,但它让我难过。
If I need to find an address, and the strings I am searching can be of the form 如果我需要找到一个地址,我搜索的字符串可以是表格
address_is_after_123
- or -
123_address_is_before
Note, there could be more than two permutations, but I'm hoping a solution for two permutations could be extended to more. 注意,可能有两个以上的排列,但我希望两个排列的解决方案可以扩展到更多。
I could simply create multiple regexes, but I'd ideally like a single regex. 我可以简单地创建多个正则表达式,但我理想地喜欢一个正则表达式。 The best I've got is:
我得到的最好的是:
m = re.match("(?:address_is_after_(\d+)|(\d+)_address_is_before)",text)
This works, but the I have to test whether m.group(1) or m.group(2) has the value. 这是有效的,但我必须测试m.group(1)或m.group(2)是否具有该值。 Is there a way to write the regex so that if it matches I can grab the address without additional processing?
有没有办法编写正则表达式,以便如果它匹配我可以抓住地址而无需额外处理?
You could do it with lookarounds, provided that the length of the lookbehind ( "address_is_after_"
) is constant: 只要lookbehind的长度(
"address_is_after_"
)是常量,你就可以用外观来做到这一点:
>>> m = re.search(r"(?<=address_is_after_)\d+|\d+(?=_address_is_before)",text)
>>> m.group(0)
'123'
You don't need to test which group has the match. 您无需测试哪个组具有匹配项。 An unmatched group returns
None
, which is treated as false by or
: 不匹配的组返回
None
,通过or
将其视为false
>>> for text in ["address_is_after_123", "123_address_is_before"]:
... m = re.match("(?:address_is_after_(\d+)|(\d+)_address_is_before)",text)
... print(m.group(1) or m.group(2))
...
123
123
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.