简体   繁体   English

Python 正则表达式替换,但前提是正则表达式之前有两个或更多字符

[英]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.我有一个模式: "two_or_more_characters - zero_or_more_characters"我想用"two_or_more_characters"替换它,其中"-"是破折号。

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 .例如对于t = "red-fox"我们会得到red But it does not work as needed for example: t = "r-fox" .但它不能按需要工作,例如: t = "r-fox" The result is r but I am looking for way to keep r-fox instead.结果是r但我正在寻找保留r-fox的方法。

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例如对于t = "r-fox-dog"结果应该是r-fox

Use a backref - that's the thing in the () in the regular expression, and \1 to "paste" it.使用 backref - 这就是正则表达式中 () 中的内容,并使用 \1 来“粘贴”它。 I think this works well enough:我认为这很好用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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