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

时间:2018-05-16 18:10:41

标签: 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,我的正则表达式是:

re.compile(r"/Orion/PatchDetails/process_form.+?release=80102000.*\n.*\n.*\n.*\n.*\n.*\n.*zip[^\"]*", re.MULTILINE)

我想要的输出是:

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&']

我正在提取一个发布列表,然后将它们作为搜索条件应用于下载网址。我通常会接受不同的解决方案。但是我想保持这个范围使用正则表达式,因为这是我使用的标签,如果这是一个错误使用正则表达式让我知道

任何人都可以帮助我,不仅仅是优化这个,而是使用所说的正则表达式向我解释逻辑。

TLDR:我需要将一个主要模式与一个变量匹配(80102000是本例中的变量)忽略\ n直到我的第二个模式匹配。

模式1:/Orion/PatchDetails/process_form.+?release=80102000 需要......之间的文字 模式2:*zip[^\"]*

先谢谢你了!

4 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

map(lambda line: re.search(expr,line), iterable_containing_lines)

可能会做你想要的。您将获得一个只包含在正则表达式上成功的行的地图对象(可迭代)。

答案 2 :(得分:0)

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,让.包含\n。对于正则表达式,这可以让您匹配多行

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

答案 3 :(得分:0)

我改进了这项工作以适应各种各样的问题而且我的代码是稳定的并且在我的代码中工作:

regex = re.compile('/Orion/PatchDetails/process_form.+?release=' + patch_info['Release'] + '.*?"((https)s?://.*?)"', re.DOTALL)