以下tex的正则表达式应该是什么

2024-09-30 20:17:37 发布

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

<p class="body">
  Giving the meeting of NITI Aayog in New Delhi a miss, West Bengal Chief        Minister and Trinamool Congress chairperson Mamata Banerjee said in Bardhaman on Wednesday that the Centre should withdraw the land acquisition ordinance.
</p>

只是为了得到<p class="body"></p>之间的内容

re.search(">(.+?)<",text)返回None


Tags: oftheinnewbodyclasswestmeeting
1条回答
网友
1楼 · 发布于 2024-09-30 20:17:37

默认情况下,.不包含新行,因此当前正则表达式不匹配。你知道吗

re.DOTALL
Make the '.' special character match any character at all, including a newline; without this flag, '.' will match anything except a newline.

这个文档有一个修饰符列表,可以用来改变regex的函数https://docs.python.org/2/library/re.html#re.S。你知道吗

因此.+?只匹配以下内容而不匹配修饰符:

<p class="body">

使用修饰符,您将获得:

<p class="body">
  Giving the meeting of NITI Aayog in New Delhi a miss, West Bengal Chief        Minister and Trinamool Congress chairperson Mamata Banerjee said in Bardhaman on Wednesday that the Centre should withdraw the land acquisition ordinance.
</p>

不过,您应该考虑为此使用解析器。此正则表达式将因搜索字符串中的任何其他元素而失败,例如:

<p class="body">
  Giving the meeting of <em>ITI</em> Aayog in New Delhi a miss, West Bengal Chief        Minister and Trinamool Congress chairperson Mamata Banerjee said in Bardhaman on Wednesday that the Centre should withdraw the land acquisition ordinance.
</p>

您可以在操作here中看到这一点。你知道吗

相关问题 更多 >