How To Ignore Unmatched Group In A String In Re Python?
I have a string input of s = 'horse dog cat bear' for which I want to change the order of the words as cat horse dog bear The corresponding re would be >>> import re &g
Solution 1:
Try
r'(horse )((?:dog )?)(cat )(bear)'
I.e. make the contents of the capturing group optional, not the group itself.
Post a Comment for "How To Ignore Unmatched Group In A String In Re Python?"