【问题标题】:Extracting substring from PDF raw text using regex使用正则表达式从 PDF 原始文本中提取子字符串
【发布时间】:2019-05-31 02:12:26
【问题描述】:

我试图从 pdf 文档中提取具有罗马索引的小节。

例如这是文档的一部分,

\n1.1\n \n范围\n \n此 PTS 指定了\n \n要求\n以及分类、验证的建议\n\n功能\nions。\n \n范围包括以下内容:\n \ni。 \n \n半\n-\n定量 SIL 分类\n \nii.\n \n虚假行程分析\n \niii.\n \n概率和架构 SIL 验证\n \niv.\n \n建议\n \n针对 SIL 差距关闭'

我想要的只有下面:

此 PTS 指定了\n\n分类、验证的要求\n和建议\n\n功能\nions。\n\n范围包括以下内容:\n\ni。\n\n半\n-\n定量 SIL 分类\n \nii.\n \n虚假行程分析\n \niii.\n \n概率和架构 SIL 验证\n \niv.\n \n建议\n \n用于 SIL 差距缩小

我需要罗马索引之前的句子以及罗马索引中的内容。

不过,也有像下面这样的情况

3.1.3\n \n做\nc\说明\n \n必需\n \nT\nh\ne\n \nl\nat\ne\ns\nt\n \n问题\n \nof\n \nt \nh\ne\n \nf\no\nllo\nw\ni\nng\n \ndocume\nn\nts\n \nshall\n \nbe\n \nav\na\nilab\nl\ne\n \nto \n \nthe\n \nte\na\nm\n \np\ne\nrf\no\nrm\ni\nng\n \nt\nh\ne \nc\nl\nass\ni\nf\ni\ncati \no\nn:\n \ni.\n \n强制性参考文件\n \na)\n \n因果矩阵 (CEM)\n \nb)\n \n管道和仪表图 (P&ID) 或过程和实用程序工程\n流程图 (PEFS)\n \nc)\n \nHAZOP 报告\n \nd)\n \nIPF 可靠性数据\n \nii.\n \n其他参考文件\n \na)\n \n工艺流程图(PFD) 或 Process Fl\now Scheme (PFS)\n \nb)\n \n工厂布局图\n \nc)\n \n过程保护流程图 (PSFS)\n \nd)\n \n控制说明\n \ne)\n \n联锁/ ESD逻辑图\n \nf)\n \n设备布局图\n \ng)\n \n维护和检查数据\n \nh)\n \n工厂历史数据\n \n \ nT\nh\ne\n \nl\ni\ns\nt\n \na\nb\no\nve\n \nis\n \nn\no\nt\n \ne\nx\nh\na\nu\恩斯蒂\nv\ne。任何\n \not\nh\ne\nr\n \ndo\nc\nu\nm\ne\nn\nt\ns\n/ \nd\nr\na\nw\nin\ng\ns\n \nreq \nu\nir\ne\nd\n \nf\no\nr\n \nt\nhe \nc\nom\np\nletion\n \no\nf the\n \nIPF\n \ns\nt\nu\ nd\ny\n \ns\nh\na\nll\n \nbe\n \nf\nu\nr\nn\完成\n \nas\n \na\nn\nd\n \nw\nhen\n \ nre\nq\nui\nr\ne\nd\n.\n \n

我已将 pdf 转换为原始文本,并成功提取了文档的一部分。

regx = re.compile( '\.\n \n.+?:\n \n',re.DOTALL)
find = str(txt)
indexhead.append((regx.findall(find)))

以上代码只能提取标题,不能同时提取罗马索引

。\n \n范围包括以下内容:\n \n

我正在尝试根据模式进行提取,但我想一些条件规则可能会有所帮助。

【问题讨论】:

    标签: python regex text regex-lookarounds data-processing


    【解决方案1】:

    如果我对问题的理解正确,我们只想取出罗马索引,得到整个段落,我们可以从一个简单的表达式开始,例如:

    .+[0-9]\.?.+?([A-Z][a-z].*)
    

    然后随着新案例的出现,我们只需使用逻辑 OR 并添加其他规则。

    Demo

    测试

    # coding=utf8
    # the above tag defines encoding for this document and is for Python 2.x compatibility
    
    import re
    
    regex = r".+[0-9]\.?.+?([A-Z][a-z].*)"
    
    test_str = ("\\n1.1\\n \\nSCOPE\\n \\nThis PTS specifies the\\n \\nrequirements \\nand recommendations for Classification, Verification \\n\\nFunct\\nions.\\n \\nThe scope includes the following:\\n \\ni.\\n \\nSemi\\n-\\nquantitative SIL classification\\n \\nii.\\n \\nSpurious trip analysis\\n \\niii.\\n \\nProbabilistic and architectural SIL verification\\n \\niv.\\n \\nRecommendations\\n \\nfor SIL gap closure'\n\n"
        "3.1.3\\n \\nDo\\nc\\numentation\\n \\nrequired\\n \\nT\\nh\\ne\\n \\nl\\nat\\ne\\ns\\nt\\n \\nissue\\n \\nof\\n \\nt\\nh\\ne\\n \\nf\\no\\nllo\\nw\\ni\\nng\\n \\ndocume\\nn\\nts\\n \\nshall\\n \\nbe\\n \\nav\\na\\nilab\\nl\\ne\\n \\nto\\n \\nthe\\n \\nte\\na\\nm\\n \\np\\ne\\nrf\\no\\nrm\\ni\\nng\\n \\nt\\nh\\ne \\nc\\nl\\nass\\ni\\nf\\ni\\ncati\\no\\nn:\\n \\ni.\\n \\nMandatory reference document\\n \\na)\\n \\nCause and effect matrices (CEM)\\n \\nb)\\n \\nPiping and Instrument Diagram (P&ID) or Process and utility engineering \\nflow schemes (PEFS)\\n \\nc)\\n \\nHAZOP report\\n \\nd)\\n \\nIPF reliability data\\n \\nii.\\n \\nOther reference document\\n \\na)\\n \\nProcess Flow Diagram (PFD) or Process Fl\\now Scheme (PFS)\\n \\nb)\\n \\nPlant layout drawing\\n \\nc)\\n \\nProcess safeguarding flow schemes (PSFS)\\n \\nd)\\n \\nControl narratives\\n \\ne)\\n \\nInterlocks/ ESD logic diagram\\n \\nf)\\n \\nEquipment layout diagram\\n \\ng)\\n \\nMaintenance and Inspection Data\\n \\nh)\\n \\nPlant historian data\\n \\n \\nT\\nh\\ne\\n \\nl\\ni\\ns\\nt\\n \\na\\nb\\no\\nve\\n \\nis\\n \\nn\\no\\nt\\n \\ne\\nx\\nh\\na\\nu\\nsti\\nv\\ne. Any\\n \\not\\nh\\ne\\nr\\n \\ndo\\nc\\nu\\nm\\ne\\nn\\nt\\ns\\n/ \\nd\\nr\\na\\nw\\nin\\ng\\ns\\n \\nreq\\nu\\nir\\ne\\nd\\n \\nf\\no\\nr\\n \\nt\\nhe \\nc\\nom\\np\\nletion\\n \\no\\nf the\\n \\nIPF\\n \\ns\\nt\\nu\\nd\\ny\\n \\ns\\nh\\na\\nll\\n \\nbe\\n \\nf\\nu\\nr\\nn\\nished\\n \\nas\\n \\na\\nn\\nd\\n \\nw\\nhen\\n \\nre\\nq\\nui\\nr\\ne\\nd\\n.\\n \\n")
    
    subst = "\\1"
    
    # You can manually specify the number of replacements by changing the 4th argument
    result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
    
    if result:
        print (result)
    
    # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
    

    正则表达式

    如果不需要此表达式,可以在 regex101.com 中修改/更改。

    正则表达式电路

    jex.im 可视化正则表达式:

    【讨论】:

    • 是的,这个解决方案可以解决上面提到的情况,但是我有一个与此不同的情况,如下\n3.1.4\\n \\nIPF\\n \\nc\\nla\\ns\\nsific\\na\\ntion\\n \\nte\\na\\nm\\n \\nThe\\n \\nt\\neam\\n \\np\\ne\\nr\\nf\\no\\nrming\\n \\nt\\nh\\ne\\n \\nI\\nP\\nF\\n \\nc\\nl\\na\\ns\\ns\\nif\\ni\\nc\\nat\\ni\\no\\nn\\n \\nshall\\n \\ncomprise the following\\n:\\n \\ni.\\n \\nP\\nrocess Technologists/ \\nProcess Engineer\\n \\nii.\\n \\nOperations shift leader or above
    • 但是它也可能发生在字符串的中间,例如\\nE = Extensive impact\\n \\nThe recommended m\\nethod to achieve the consequences of failure on demand description is \\nas follows:\\n \\ni.\\n \\nDescribe the \\npurpose\\n \\n(or design intent) of the IPF;\\n \\nii.\\n \\nDescribe the \\ncause(s)of the demand on the IPF, the so called demand scenarios \\ni.e\\n.\\n \\nprocess control layer failure \\ne.g. con\\ntrol valve failure, \\nO\\nperator error, loss of \\nfeed 我正在考虑将: 包含在其中一个条件中,您认为这有意义吗?
    • 我也不明白你为什么把\n转换成\\n?是强制性的吗?如果我想保留为 \n 怎么办?
    • 感谢@Emma,我已经改进和修改了正则表达式,并在此处发布了答案。我只设法在“:i”之后提取任何内容。因此,我将继续即兴创作,以便在罗马 id​​ice 小节之前加入解释句。
    【解决方案2】:

    经过一番探索,以下是最接近我想要实现的解决方案:

    regx = re.compile( ': \ni(?:(?!\n[A-Z]).).*?\.\n\d\.|:\ni(?:(?!\n[A-Z]).).*?\.\n\d\.',re.DOTALL)
    find = str(cleanSectionContent2[req])
    

    它会检测那些以 ':i' 开头的情况。并以部分标题“\n\d.”结尾,但它无法检测到所有情况,因此我将在此处更新更多解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-14
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多