python - 使用 re 模块替换文本时出错
问题描述
我正在尝试替换字符串中的一些文本。我有“微软公司”。我想将其替换为“Microsoft Incorporation”。为此,我尝试过
company = 'Microsoft Inc.'
key = 'Inc.'
pattern = r'\b' + key + r'\b'
# pattern = r'\b' + re.escape(key) + r'\b'
_change = 'Incorporation'
y = re.sub(pattern, _change, company)
print(y)
我没有看到任何替代品。但是当我删除“。” 来自公司和密钥然后它工作正常,但是当有“。” 在字符串中它不会改变任何东西。
解决方案
推荐阅读
- spring - 如何将一个字段的值分配给 Thymeleaf 中其他对象的另一个字段?
- apache-spark-sql - 如何使用 Spark sql 在 Databricks 中使用内部联接更新 Databricks Delta 表
- python - ROC AUC 值为 0
- layout - 为具有约束的图节点创建布局
- powershell - 无法获取群组名称
- python - ValueError:解析日期时时间数据与格式不匹配
- makefile - Makefile:如何生成以当前目录命名的 zip 文件
- javascript - 使用 JavaScript try/catch,什么是不处理 catch 错误消息的干净方法?
- mysql - sequelize 外键返回为“Null”
- properties - 撇号 CMS 传播编辑:所有子小部件和部件的区域为假