Python - 正则表达式匹配多个模式多行

2024-06-06 18:16:10 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我刚刚开始构建regex,总体上已经取得了一些巨大的成功。但是我有一个特别的案子让我很困惑。我可以得到我想要的匹配,但它不漂亮,无论在任何方面,形状或形式。在

我是正则表达式匹配一些html文档与多行。我需要从这些文档中提取信息块,这些信息块与每个块中的变量模式相匹配,然后将所需的信息拉入。在

有多个html块包含我需要的信息,如下所示:

<td headers="col0" class="OraTableCellNumber" style=";" nowrap="1"  valign="top" ><a href='/Orion/PatchDetails/process_form?patch_num=6880880&aru=13915384&release=80101000&plat_lang=226P&patch_num_id=979662&' title="View Patch Details">6880880</a></td>
<td headers="col0" class="OraTableCellText" style=";"   valign="top" ><b>Universal Installer</b>: Patch<br>OPatch 9i, 10.1</td>
<td headers="col0" class="OraTableCellText" style=";"   valign="top" >10.1.0.0.0</td>
<td headers="col0" class="OraTableCellText" style=";" nowrap="1"  valign="top" >08-JUL-2011</td>
<td headers="col0" class="OraTableCellText" style=";"   valign="top" >25M</td>
<td headers="col0" class="OraTableCellText" style=";text-align: center;"   valign="middle" width="15"><a href='javascript:showDetails("/Orion/Readme/process_form?aru=13915384&no_header=1")'><img src="/olaf/images/forms/readme.gif" valign=bottom border=0 title="View Readme" alt="View Readme"></a></td>
<td headers="col0" class="OraTableCellText" style=";text-align: center;"   valign="middle" width="15"><a href="https://updates.oracle.com/Orion/Download/process_form/p6880880_101000_Linux-x86-64.zip?aru=13915384&file_id=42098007&patch_file=p6880880_101000_Linux-x86-64.zip&"><img src="/olaf/images/forms/download.gif" valign=bottom border=0 title="Download Now" alt="Download Now"></a></td></tr>
<tr class="OraBGAccentLight" height="28" onMouseOver="javascript:setRowClass(this, 'highlight', 1);" onMouseOut="javascript:setRowClass(this, 'highlight', 0);">

我目前正在Python中工作,我的regex是:

^{pr2}$

我想要的输出是:

20180516140046EDT - DEBUG - ['/Orion/PatchDetails/process_form?patch_num=6880880&aru=13116068&release=80102000&plat_lang=226P&patch_num_id=979663&\' title="View Patch Details">6880880</a></td>\n<td headers="col0" class="OraTableCellText" style=";"   valign="top" ><b>Universal Installer</b>: Patch<br>OPatch 10.2</td>\n<td headers="col0" class="OraTableCellText" style=";"   valign="top" >10.2.0.0.0</td>\n<td headers="col0" class="OraTableCellText" style=";" nowrap="1"  valign="top" >18-NOV-2010</td>\n<td headers="col0" class="OraTableCellText" style=";"   valign="top" >26M</td>\n<td headers="col0" class="OraTableCellText" style=";text-align: center;"   valign="middle" width="15"><a href=\'javascript:showDetails("/Orion/Readme/process_form?aru=13116068&no_header=1")\'><img src="/olaf/images/forms/readme.gif" valign=bottom border=0 title="View Readme" alt="View Readme"></a></td>\n<td headers="col0" class="OraTableCellText" style=";text-align: center;"   valign="middle" width="15"><a href="https://updates.oracle.com/Orion/Download/process_form/p6880880_102000_Linux-x86-64.zip?aru=13116068&file_id=34545782&patch_file=p6880880_102000_Linux-x86-64.zip&']

我正在提取一个版本列表,然后将它们作为搜索条件来提取下载URL。我通常会接受不同的解决方案。不过,我想保留这一范围使用正则表达式,因为这是我使用的标记,如果这是一个严重错过使用正则表达式让我知道

有谁能帮助我不仅优化这一点,而且解释给我的逻辑使用上述建议的正则表达式。在

TLDR:我需要将前导模式与变量(本例中的变量为80102000)匹配,直到匹配到第二个模式为止。在

模式1:/Orion/PatchDetails/process_form.+?release=80102000 需要文本介于。。。 模式2:*zip[^\"]*

提前谢谢你!在


Tags: formviewstyletopprocessreadmeclasspatch
3条回答
map(lambda line: re.search(expr,line), iterable_containing_lines)

可能会做你想做的事。您将获得一个map对象(它是iterable),它只包含在regex上成功的行。在

流行的观点是用正则表达式解析HTML不是一个好主意,请参见https://stackoverflow.com/a/1732454/9778302

import re

regex = r"""
  Orion/PatchDetails/process_form.+?release=\d+       
  (.+)   # use this as your match
  zip[^\"]
  """

matches = re.compile(regex, re.MULTILINE | re.DOTALL | re.VERBOSE)

添加re.DOTALL,让.包含{}。对于regex,这允许您匹配多行

https://regex101.com/r/jBwq20/1

相关问题 更多 >