python - Python正则表达式匹配关键字的所有变体,除非前面有大写单词
问题描述
我正在寻找一个 Python 正则表达式来匹配关键字的所有变体,除非前面有一个大写单词 - > 除非那个大写单词是句子的开头。也排除括号之间的单词。
例如:
keyword = 'public record'
string1 = 'Hello. His public records are available at city hall.' #match public records His is the start of a sentence so we ignore that it is capitalized and match
string2 = 'his records are at Newsom Public Record DataBase' #nomatch
string3 = 'Public records may be available online' #match Public records
string4 = '[public records](http:/....)' #nomatch
到目前为止,我已经尝试过:
pattern = f'(?<!\[)(?i)\\w*{keyword}\\w*' #Doesn't take into account preceding capitalized words
pattern = f'(?<![A-Z][\w-]\s)(?<!\[)(?i)\\w*{keyword}\\w*' #Doesn't work for cap words > 2 chara
解决方案
您可以指定各种允许的开头,即句子开头 + 大写单词、非大写单词或字符串开头,然后断言关键字后跟前瞻:
pattern = r'(\. [A-Z]\w* |\W[^A-Z]\w* |^)(?=[pP]ublic [rR]ecord)'
推荐阅读
- c# - 使用 Ajax 更新两个下拉列表过滤器的值 C# MVC
- c# - 将 DataSet 从 WCF 返回到 .Net Core 应用程序会将 DataSet 转换为 ArrayOfXElements 类型
- javascript - 如何从对象中的数组中删除元素并返回新对象?
- php - 为什么不在 php 中使用数据库函数类
- python - 在 Python3 中操作 JSON 配置文件中的变量的最佳方法
- strapi - 登录后如何修改密码?
- javascript - 如何在程序中间显示代码内的确认框
- java - 在 Appium 中使用 Xpath 查找元素的父元素
- python - 如何解释图表
- r - 计算 R Dataframe 中每个 ID 的每日差异