Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Regular Expression - Named Group Not Fully Matching

Tags:

python

regex

I have the following Python regex pattern:

(?P<key>.*)(?P<operator><=>|=|>=|>|<=|<|!=|<>)(?P<value>.*)

and my input string example is: this!=that, but the != is not getting matched as a group:

{u'operator': '=', u'key': 'this!', u'value': 'that'}

Can you please help me match against the full operator != in this example using the above regex pattern with some explanation on why my original pattern did not work? Thank you in advance!

like image 201
Preston Connors Avatar asked Feb 17 '26 02:02

Preston Connors


1 Answers

You need to use lazy matching with the first capturing group, otherwise, .* will "eat" the first symbol since it is greedy and can also match any symbols in your alternatives:

(?P<key>.*?)(?P<operator><=>|!=|>=|<=|<>|[=><])(?P<value>.*)

See demo

I have also rearranged the alternatives so that they go from the longest to the shortest. This might be important since regex is processing from left to right, and thus, we should check for the longest option first.

And the last three alternatives can be shrunk into a character class [=><] to lessen the backtracking.

like image 120
Wiktor Stribiżew Avatar answered Feb 19 '26 14:02

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!