简体   繁体   中英

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

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

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM