python - 如何忽略lookbehind中的特定单词?
问题描述
完整字符串:
See Item 1A. Risk Factors – Regulatory, Compliance and Legal on page 13.
**Here Item 1A. Risk Factors – Regulatory, Compliance and Legal on page 13.**
我正在使用这段代码来获取句子**
:
(?i)(?<=\D|![see])Item.+?1A.+?Risk Factors(?=\D)(.+?)(?=Item 1B|$)
通过给出特定的词See
来忽略它。
但它仍然采用两个句子。
解决方案
如果你想在后面添加一个量词)(?<!\bSee\b.*)
,你可以使用PyPi regex 模块。
(?<=\D)(?<!\bSee\b.*)Item.+?1A.+?Risk Factors(?=\D)(.+?)(?=Item 1B|$)
假设**
那里表明句子之间的差异,您可以看到第二个是匹配的**
,因为结尾是。
例如
import regex
pattern = r"(?i)(?<=\D)(?<!\bSee\b.*)Item.+?1A.+?Risk Factors(?=\D)(.+?)(?=Item 1B|$)"
test_str = ("See Item 1A. Risk Factors – Regulatory, Compliance and Legal on page 13.\n"
"**Here Item 1A. Risk Factors – Regulatory, Compliance and Legal on page 13.**")
m = regex.search(pattern, test_str)
print(m.group())
输出
Item 1A. Risk Factors – Regulatory, Compliance and Legal on page 13.**
另一种选择可能是匹配您不想要的内容并使用交替捕获您想要保留的内容。
See
匹配之前出现的整行,并使用模式在第 1 组Item
中捕获,后向断言不是数字。
(?:^(?:(?!\bItem\b.)*)See\b.*|((?<=\D)Item.+?1A.+?Risk Factors(?=\D).+?(?=Item 1B|$)))
推荐阅读
- python - 如何通过python中的代理使用Tumblr Api
- php - 基于 Woocommerce 中产品价格的条件短代码
- r - 在 R 中 state.name 和 state.abb 之间的转换
- python - Raspberry Pi - Firestore 时间戳问题
- react-native - 三重嵌套的 ScrollVIew 不滚动
- c++ - 如何创建一个可以使用自定义可比 C++ 的函数?
- c# - 使用 SOAP 请求中的自定义字段实现 WSSE 安全标头时出现 C# 运行时错误
- video - 为什么我无法将 4 秒 60 fps 的剪辑缩小到正好 1 秒?
- python - 如何在两个变量中存储相同的值,同时在 python 中独立更改它们?
- python - 将带有 MultiIndex 的 pandas DataFrame 与包含新标签的数据附加,但保留旧 MultiIndex 的整数位置