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

Question:

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

Asked By: illuminato

||

Answers:

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")
Answered By: Petr Blahos
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.