I'm using regex in a python script to capture a named group. The group occurs before OR after a delimiter string "S". My confusion comes from an inability to use named capturing groups twice in the same regex.
I'd like to use the following invalid (named group used twice) regex:
(?:^STD_S_)(?P<important>.+?)$|(?:^(?P<important>.+?)(?:_S_STD)$
Description:
?: non-capture group ^STD_S_ Starting with some "STD_S_" string which is a standard string plus a delimiter
?P Named important string I want
| OR
^?P stat with important _S_STD$ end with standard
I would really like the important group I capture to be named. I can remove the names and get this to work. I can also split the single expression into two expressions (one from each side of the OR) and search choose which one to use with some login in the python script.
Thanks!
EXAMPLE INPUTS
STD_S_important
important_S_STD
EXAMPLE OUTPUTS
important #returned by calling the important named group
important
regex based on comments that doesn't match the second case.
(?:(?:^STD_S_)(?P<important>.+?)$)|(?:^(?P=important)(?:_S_STD)$)