处理正则表达式中的制表符和行尾

时间:2014-04-09 10:16:09

标签: python regex

问题: 我通过执行以下操作返回EMPTY LIST:

output_list = re.findall(r'<td colspan="4" class="yellow-shade border justify">[\r\n]+(.*?)[\r\n]+', INPUTTEXT)

例如,当INPUTTEXT参数完全如下:

<tr>
            <td colspan="4" class="yellow-shade border justify">
            Online Learning Comment         
            <div class="report-action">

              <a class="add-new fb-link"  href="http://blah-blah-blah/write-report?rep[company]=768744&amp;rep[company_name]=Funky Group Services&amp;rep[responds]=1" > Services Report</a>

              <table style="float:right"><tr><td><a class="inappropriate" href="" onclick="window.open('http://blah-blah-blah/inappropriate-report?report=1379443','','toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=650,height=620'); return false">Inappropriate report?</a></td>

                  <td><a style=' margin-left:15px; float: right;' class="back" href="javascript:history.go(-1)">Back</a></td></tr></table>

            </div>

            </td>
        </tr>

必填项:

output_list =['Online Learning Comment']. 

我的步骤中缺少什么。就像我对正则表达式一样新,我认为我的reg表达式会起作用吗?任何指针都非常感激。

1 个答案:

答案 0 :(得分:1)

我尝试了您的代码并将[' Online Learning Comment']返回给我。除了\r\n之外,您可能还有其他一些不可见的符号。请尝试使用此正则表达式:

r'<td colspan="4" class="yellow-shade border justify">\s+(.*?)[\r\n]'

P.S。此外,此代码非常脆弱。首先,空格在html中没有意义,因此可以任意改变。其次,您匹配的类和属性不具有语义,并且可以在将来轻松更改。