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

zd287kbt  于 2023-02-05  发布在  其他
关注(0)|答案(3)|浏览(164)
<p>I'd like to find the string between the two paragraph tags.</p><br><p>And also this string</p>

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

cunj1qz1

cunj1qz11#

介于<p></p>之间

In [7]: content = "<p>I'd like to find the string between the two paragraph tags.</p><br><p>And also this string</p>"

In [8]: re.findall(r'<p>(.+?)</p>', content)
Out[8]: 
["I'd like to find the string between the two paragraph tags.",
 'And also this string']
bhmjp9jg

bhmjp9jg2#

Regular expressions

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

vq8itlhq3#

如果您希望字符串位于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']

相关问题