Preston Connors
Preston Connors

Reputation: 407

Python Regular Expression - Named Group Not Fully Matching

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!

Upvotes: 3

Views: 171

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626794

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.

Upvotes: 5

Related Questions