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

2024-04-26 20:14:44 发布

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

问题: 我通过执行以下操作返回空列表:

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

我在我的脚步中错过了什么。虽然我对正则表达式很陌生,但我认为我的正则表达式可以工作?非常感谢任何指点


Tags: reportoutputtrlistclassyestdhref
1条回答
网友
1楼 · 发布于 2024-04-26 20:14:44

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

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

另外,这个代码非常脆弱。首先,空白在html中没有意义,因此可以任意更改。其次,匹配的类和属性没有语义,将来很容易更改

相关问题 更多 >