I have a string that can exist in either of the following two formats within a larger body of text:
OptionalSpecificString1 1234
1234 OptionalSpecificString2
The text here is all placeholders. I'm looking for a numerical string that's either preceded or followed by a specific optional string. One of the two optional specific strings will always be present and is needed to locate and capture the numerical string-of-interest. Is there a single regex pattern that exists that can capture this behavior?
Something like:
(?:OptionalSpecificString1)? (\d+) (?:OptionalSpecificString2)?
almost does it, but doesn't require that one of the two optional strings is present, and so it could end up matching any other numerical string in the body of the text. I know I could do something like:
(OptionalSpecificString1 (\d+)|(\d+) OptionSpecificString2)
but I guess I'm just wondering if there's something a little more elegant. I'm doing this with the Python re
module, so code can be a bit simpler too when I can express a single capture group for the same pattern.