Looking for a python regex pattern. 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. 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: