[英]Regex pattern for matching entire word if it have a ; in the word in python
我正在尝试从文本中删除一些垃圾,并希望删除所有2个字符中间带有“,”的单词。 我尝试过以下两种表达方式
r'\s.*;.*\s' and r'\s.*\W.*\s'
在本文中
'the cat as;asas was wjdwi;qs at home'
而且似乎错过了一些空白,
'cat as;asas was wjdwi;qs at '
当我需要的时候
'the cat was at home'
一个简单的解决方案是不使用正则表达式:
s = 'the cat as;asas was wjdwi;qs at home'
res = ' '.join(w for w in s.split() if ';' not in w)
# the cat was at home
您可能需要更复杂的检查,但首先将其拆分为“单词”,然后将检查应用于每个“单词” ...
您可以使用此:
re.sub(r'(?i)\s?[a-z]+;[a-z]+\s?', ' ', yourstr)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.