Home > Net >  In a regular expression, match one thing or another, or both while maintaining group names
In a regular expression, match one thing or another, or both while maintaining group names

Time:07-22

I wish to expand on the discussion on the following question

In a regular expression, match one thing or another, or both

with a new condition that I want to name the groups consistently. Something like this -

((?P<A>A)|(?P<B>B)) | ((?P<A>A)(?P<B>B))

which triggers re.error: redefinition of group name ... in Python.

Additional info in case it helps: In my specific application, A is a date and B is a time. Basically I want to allow users to enter a date, or a time, or a date followed by a time (which is why A?B? would not work for me as I do not want it empty).

The group name feature of regex turns out to be very handy when it comes to parsing the date and/or time info to datetime objects and functionalities. This is why I want to be able to keep naming A A and B B even when the pattern repeats. This makes the accepted solution in the cited discussion not work for me.

Edit: Yes, I agree that repeating a pattern is not a good idea in the first place especially when date and time are already messy.

CodePudding user response:

If you want the possibility to match either one of the groups or both groups without matching an empty string, you can assert a non whitespace char to the right of the current position:

(?=\S)((?P<A>A)?(?P<B>B)?)

See a regex demo.

  • Related