Home > Net >  Python Regex replace but only if two or more characters precede regex expression
Python Regex replace but only if two or more characters precede regex expression

Time:12-12

I have a pattern: "two_or_more_characters - zero_or_more_characters" and I want to replace it with "two_or_more_characters", where "-" is a dash.

I created regex for it:

re.sub(r'-[\w(){}\[\],.?! ] ', '', t)

and it works as expected for some cases. For example for t = "red-fox" we will get red. But it does not work as needed for example: t = "r-fox". The result is r but I am looking for way to keep r-fox instead.

If text has more then one dash then we need to remove text only after last dash. For example for t = "r-fox-dog" the result should be r-fox

CodePudding user response:

Use a backref - that's the thing in the () in the regular expression, and \1 to "paste" it. I think this works well enough:

re.sub(r'(.{2,})-.*', r'\1', "ss-fox")
  • Related