python - 如何使用 re.DOTALL 在多行文本中搜索正则表达式模式?
问题描述
我是一名律师和 python 初学者,所以我既 (a) 愚蠢又 (b) 完全不在我的车道上。
我正在尝试将正则表达式模式应用于文本文件。该模式有时可以跨越多条线。我对文本文件中的这些行特别感兴趣:
Considered and decided by Hemingway, Presiding Judge; Bell,
Judge; and \n
\n
Dickinson, Emily, Judge.
我想单独寻找,提取,然后打印评委的名字。到目前为止,我的代码如下所示:
import re
def judges():
presiding = re.compile(r'by\s*?([A-Z].*),\s*?Presiding\s*?Judge;', re.DOTALL)
judge2 = re.compile(r'Presiding\s*?Judge;\s*?([A-Z].*),\s*?Judge;', re.DOTALL)
judge3 = re.compile(r'([A-Z].*), Judge\.', re.DOTALL)
with open("text.txt", "r") as case:
for lines in case:
presiding_match = re.search(presiding, lines)
judge2_match = re.search(judge2, lines)
judge3_match = re.search(judge3, lines)
if presiding_match or judge2_match or judge3_match:
print(presiding_match.group(1))
print(judge2_match.group(1))
print(judge3_match.group(1))
break
当我运行它时,我可以得到 Hemingway 和 Bell,但是在两个换行符之后,我得到一个“AttributeError:'NoneType' object has no attribute 'group'”用于第三个判断。
经过反复试验,我发现我的代码只读取第一行(直到“Bell, Judge; and”)然后退出。我以为 re.DOTALL 会解决它,但我似乎无法让它工作。
我已经尝试了一百万种方法来捕获换行符并获取整个内容,包括 re.match、re.DOTALL、re.MULTILINE、"".join、"".join(lines.strip()) 和任何东西否则我可以靠墙扔棍子。
几天后,我屈服于寻求帮助。谢谢你能做的任何事情。
(顺便说一句,我没有运气让正则表达式与 ^ 和 $ 字符一起使用。它似乎也讨厌 . 正则表达式中的 . 转义。)
解决方案
您正在单行传递,因为您正在遍历 . 引用的打开文件case
。除了单行文本外,正则表达式永远不会传递任何内容。您的正则表达式每个都可以匹配某些行,但它们并不都匹配同一行。
你必须阅读不止一行。如果文件足够小,只需将其作为一个字符串读取:
with open("text.txt", "r") as case:
case_text = case.read()
然后将您的正则表达式应用于该字符串。
或者,您可以单独测试每个匹配对象,而不是作为一个组,并且只打印匹配的对象:
if presiding_match:
print(presiding_match.group(1))
elif judge2_match:
print(judge2_match.group(1))
elif judge3_match:
print(judge3_match.group(1))
但是您必须创建额外的逻辑来确定何时完成从文件中读取并跳出循环。
请注意,您匹配的模式不会跨行中断,因此DOTALL
此处实际上不需要该标志。你确实匹配文本,所以如果你使用.*
,你会冒着匹配太多DOTALL
的风险:
>>> import re
>>> case_text = """Considered and decided by Hemingway, Presiding Judge; Bell, Judge; and
...
... Dickinson, Emily, Judge.
... """
>>> presiding = re.compile(r'by\s*?([A-Z].*),\s*?Presiding\s*?Judge;', re.DOTALL)
>>> judge2 = re.compile(r'Presiding\s*?Judge;\s*?([A-Z].*),\s*?Judge;', re.DOTALL)
>>> judge3 = re.compile(r'([A-Z].*), Judge\.', re.DOTALL)
>>> presiding.search(case_text).groups()
('Hemingway',)
>>> judge2.search(case_text).groups()
('Bell',)
>>> judge3.search(case_text).groups()
('Considered and decided by Hemingway, Presiding Judge; Bell, Judge; and \n\nDickinson, Emily',)
我至少会替换[A-Z].*
为[A-Z][^;\n]+
, 以至少排除匹配的;
分号和换行符,并且只匹配至少 2 个字符长的名称。完全放下DOTALL
旗帜:
>>> presiding = re.compile(r'by\s*?([A-Z][^;]+),\s+?Presiding\s+?Judge;')
>>> judge2 = re.compile(r'Presiding\s+?Judge;\s+?([A-Z][^;]+),\s+?Judge;')
>>> judge3 = re.compile(r'([A-Z][^;]+), Judge\.')
>>> presiding.search(case_text).groups()
('Hemingway',)
>>> judge2.search(case_text).groups()
('Bell',)
>>> judge3.search(case_text).groups()
('Dickinson, Emily',)
您可以将三种模式合二为一:
judges = re.compile(
r'(?:Considered\s+?and\s+?decided\s+?by\s+?)?'
r'([A-Z][^;]+),\s+?(?:Presiding\s+?)?Judge[.;]'
)
它可以一次性找到您输入中的所有评委.findall()
:
>>> judges.findall(case_text)
['Hemingway', 'Bell', 'Dickinson, Emily']
推荐阅读
- deserialization - 反序列化不正确的字符串数组?
- tvos - TvOs:有没有办法知道电视何时关闭/打开?
- c# - 如何模拟ctrl+enter
- firebase - Firebase 警报订阅:没有 Crashlytics 设置
- python - 如果我想预测数字序列中的下一个元素,我需要将什么作为第二个参数传递给 Keras 的 fit 方法?
- ios - 使用应用内购买测试 macos Catalyst 应用的适当方法?
- huawei-mobile-services - 应用关闭时HMS Push Kit处理数据消息(React Native)
- c# - Microsoft Azure SSO 不让我登录
- postgresql-9.5 - 无法使用上一个数组项中的值将新项添加到数组中
- r - 当样本量大于总体大小时如何重复采样而不放回