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

时间:2017-05-30 09:31:23

标签: python django

function getWaterMeterList() {
    // alert("ON");
    var BillingPeriod = $('#BillingPeriod').val();
    $.ajax({
        url: '/DataEntryWater/WaterMeterAlphaListReport',
        type: 'POST',
        data: { 'BillingPeriod': BillingPeriod },
        dataType: 'json',
        success: function (a) {
            $(location).attr('href', a)
            a.preventDefault();
        },
        error: function (err) {
        }
    });
}

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

3 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:0)

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

答案 2 :(得分: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']