[英]How to exclude a word while using re.sub in Python?
我有相当多的所有格,我正在尝试相应地清理它们。
我想在删除它们时排除一两个词。 到目前为止,这是我不工作的例子:
import re
re.sub(r"(s')", '', "united states' and washingtons' and civilians' and qur'an")
在上述情况下,我希望“状态”不要被切掉。 感谢您的时间和帮助!
您可以在后面使用否定的外观, '(?<!something)'
告诉正则表达式只对"s'"
后面没有'state'
的单词进行切片:
import re
print(re.sub(r"(?<! state)s'", '', "united states' and washingtons' and civilians' and qur'an"))
Output:
united states' and washington and civilian and qur'an
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.