Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用正则表达式解析HTML表行_Html_Regex - Fatal编程技术网

使用正则表达式解析HTML表行

使用正则表达式解析HTML表行,html,regex,Html,Regex,我已经看过这篇文章了。作为交给我的任务的一部分,我别无选择,只能对HTML使用正则表达式 我有HTML代码,并分别尝试喜欢 <td class="a-nowrap"> <span class="a-letter-space"></span><span>13</span> </td> 13 我已经能够使用以下正则表达式得到13: <td class="a-nowrap"&g

我已经看过这篇文章了。作为交给我的任务的一部分,我别无选择,只能对HTML使用正则表达式

我有HTML代码,并分别尝试喜欢

 <td class="a-nowrap">

          <span class="a-letter-space"></span><span>13</span>

        </td>

13
我已经能够使用以下正则表达式得到13

<td class="a-nowrap">\s*<span class="a-letter-space"></span><span>(\d*)</span>\s*</td>
\s*(\d*)\s*
同样地 从


使用正则表达式获得了5个星号

<td class="a-nowrap">\s*<a class="a-link-normal" [^>]*>\s*(.*)</a>\s*</td>
\s*
13
2.

如何使用正则表达式提取5星和13星?

如果您不想使用HTML解析器,请使用一个接一个的正则表达式,或者在两个模式之间添加
*
这个,我对正则表达式做了一些修改,因为它不能正常工作:

首先启用点调用标记,然后使用此标记:

<td class="a-nowrap">\s*<a class="a-link-normal" [^>]*>\s*(\d star).*<td class="a-nowrap">\s*<span class="a-letter-space"></span><span>(\d*)</span>\s*</td>

使用上面的表达式,它将类似于[('5星','',('13')],但我想要类似于[('5星','13'),“|”的表达式,或者制造这个麻烦的表达式。有什么想法吗?@naveenyadav这很奇怪,因为我使用了您提供的图案,刚刚添加的图案或它们之间的图案,因此图案将捕捉**5星**和/或13星。当您单独使用这些模式时,这些模式对您有效吗?@naveenyadav好的,您几乎可以得到您想要的:)好的,让我想一想。@naveenyadav好的,您可以得到与这两种情况匹配的输出,但两种结果都是您想要的,所以您可以随心所欲地使用它们,对吗?不幸的是,我无法检查这个正则表达式如何正常工作,因为我从未将正则表达式用于HTML:(代码运行良好。感谢您的帮助。感谢您用新的较短正则表达式更新了我的答案,它适用于您提供的修改输入。
<table id="histogramTable" class="a-normal a-align-middle a-spacing-base">

  <tr class="a-histogram-row">



        <td class="a-nowrap">

          <a class="a-link-normal" title="69% of reviews have 5 stars" href="">5 star</a><span class="a-letter-space"></span>          

        </td>

        <td class="a-span10">

          <a class="a-link-normal" title="69% of reviews have 5 stars" href=""><div class="a-meter"><div class="a-meter-bar" style="width: 69.1358024691358%;"></div></div></a>

        </td>

        <td class="a-nowrap">

          <span class="a-letter-space"></span><span>13</span>

        </td>

  </tr>
  <td class="a-nowrap">

      <a class="a-link-normal" title="2% of reviews have 1 stars" href="">1 star</a><span class="a-letter-space"></span>          

    </td>

    <td class="a-span10">

      <a class="a-link-normal" title="2% of reviews have 1 stars" href=""><div class="a-meter"><div class="a-meter-bar" style="width: 2.46913580246914%;"></div></div></a>

    </td>

    <td class="a-nowrap">

      <span class="a-letter-space"></span><span>2</span>

    </td>


</table>
<td class="a-nowrap">\s*<a class="a-link-normal" [^>]*>\s*(\d star).*<td class="a-nowrap">\s*<span class="a-letter-space"></span><span>(\d*)</span>\s*</td>
>(\d star)<.+?>(\d+?)<
>>> regex.findall(string)
[(u'5 star', u'13'), (u'1 star', u'2')]