获取两个字符串之间的字符串

新手上路,请多包涵
<p>I'd like to find the string between the two paragraph tags.</p><br><p>And also this string</p>

我如何获得前两个段落标签之间的字符串?然后,如何获取第二段标签之间的字符串?

原文由 Zorgan 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 612
2 个回答

常用表达

import re
matches = re.findall(r'<p>.+?</p>',string)

以下是您在控制台中运行的文本。

 >>>import re
>>>string = """<p>I'd like to find the string between the two paragraph tags.</p><br><p>And also this string</p>"""
>>>re.findall('<p>.+?</p>',string)
["<p>I'd like to find the string between the two paragraph tags.</p>", '<p>And also this string</p>']

原文由 Isdj 发布,翻译遵循 CC BY-SA 3.0 许可协议

如果你想要 p 标签之间的字符串( 不包括 p 标签)然后添加括号到 .+? 在 findall 方法中

import re
    string = """<p>I'd like to find the string between the two paragraph tags.</p><br><p>And also this string</p>"""
    subStr = re.findall(r'<p>(.+?)</p>',string)
    print subStr

结果

["I'd like to find the string between the two paragraph tags.", 'And also this string']

原文由 Rich Rajah 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题