python - 在 Python 中使用 re.sub 时如何排除单词?
问题描述
我有相当多的所有格,我正在尝试相应地清理它们。
我想在删除它们时排除一两个词。到目前为止,这是我不工作的例子:
import re
re.sub(r"(s')", '', "united states' and washingtons' and civilians' and qur'an")
在上述情况下,我希望“状态”不要被切掉。感谢您的时间和帮助!
解决方案
您可以在后面使用否定的外观,告诉正则表达式只对后面'(?<!something)'
没有的单词进行切片:'state'
"s'"
import re
print(re.sub(r"(?<! state)s'", '', "united states' and washingtons' and civilians' and qur'an"))
输出:
united states' and washington and civilian and qur'an
推荐阅读
- apache-spark - Spark:函数上的过滤器没有被下推
- javascript - 使用 Scss 和 JS 变量对 React 项目进行主题化
- android - 如何将 .a 链接到 .so 并为 android 创建新的 .so
- reactjs - 从 State 中调用一个值到 componentDidMount() 函数 API URL
- python - 如何在 tensorflow 联合 sgd 中操作客户端梯度
- java - 扫雷的设计模式
- php - 找到字符串时正则表达式忽略匹配
- javascript - Angular 附加到路由器出口而不是替换内容
- node.js - Nodejs Azure 部署失败:ENOENT:没有这样的文件或目录,stat '/home/site/wwwroot/node_modules/.bin/semver'
- python - 包含列表的列中元素总数的新列