Zorgan
Zorgan

Reputation: 9123

Get string between two strings

<p>I'd like to find the string between the two paragraph tags.</p><br><p>And also this string</p>

How would I get the string between the first two paragraph tags? And then, how would I get the string between the 2nd paragraph tags?

Upvotes: 8

Views: 41195

Answers (3)

Rich Rajah
Rich Rajah

Reputation: 2454

If you want the string between the p tags (excluding the p tags) then add parenthesis to .+? in the findall method

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

Result

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

Upvotes: 13

itzMEonTV
itzMEonTV

Reputation: 20339

In between <p> and </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']

Upvotes: 0

Isdj
Isdj

Reputation: 1856

Regular expressions

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

The following is your text run in console.

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

Upvotes: 15

Related Questions