两个字符串之间的正则表达式文本 [英] Regex text between two strings
问题描述
我正在尝试使用正则表达式从 PDF 文本中提取数据字段.
I am trying to extract data fields from PDF texts using regex.
正文是:
"样本 EXPERIAN 客户\n2288150 - EXPERIAN 样本报告\n数据字典报告\n过滤条件:\n自定义选择\n营销元素:\n第 1 页,共 284 页\n2014-11-11 21:52:01 PM\nExperian以及此处使用的标记是 Experian 的服务标记或注册商标.\n© Experian 2014 保留所有权利.机密和专有.\n**数据字典**\n出生日期是从公共和专有文件中获得的.这些来源提供,至少是出生年份;在可用的情况下提供月份.可以提供各种详细级别的确切出生日期\n\n\n\n\n\n注意:使用 DOB 编码的记录不包括估计的年龄 (101E)\n**元素编号**\n0100\n描述\n出生日期/确切年龄\n**数据字典**\n\n\n\n\n\n\n\n\n\n\n填充,三个字节\n**元素编号**\n0000\n**描述**\n增强强制附加\n**数据字典**\n\n\n当没有足够的数据来匹配客户的记录时给我们的浓缩大师估计年龄,提供了基于同一 ZIP+4 区域内所有其他成年个体年龄的估计年龄中位数.\n\n\n\n\n\n\n00 = 未知\n**元素编号**\n0101E\n**描述**\n估计年龄\n"
字段名称以粗体显示.字段名称之间的文本是字段值.
The field names are in bold. The texts between field names are the field values.
我第一次尝试使用以下正则表达式提取描述"字段:
The first time I tried to extract the 'Description' field using the following regex:
pattern = re.compile('\nDescription\n(.*?)\nData Dictionary\n')
re.findall(pattern,text)
结果正确:
['Date Of Birth / Exact Age', 'Enhancement Mandatory Append']
但是使用相同的想法提取数据字典"字段给出了空结果:
But using the same idea to extract 'Data Dictionary' Field gives the empty result:
pattern = re.compile('\nData Dictionary\n(.*?)\nElement Number\n')
re.findall(pattern,text)
结果:
[]
知道为什么吗?
推荐答案
.
默认不匹配换行符.试试:
.
doesn't match newlines by default. Try:
pattern = re.compile('\nData Dictionary\n(.*?)\nElement Number\n', flags=re.DOTALL)
re.findall(pattern,text)
注意我是如何通过 re.DOTALL
作为<的flags
参数代码>重新编译.
Notice how I passed re.DOTALL
as the flags
argument to re.compile
.
这篇关于两个字符串之间的正则表达式文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!