8
<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?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Zorgan
  • 8,227
  • 23
  • 106
  • 207

3 Answers3

15

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>']
Isdj
  • 1,835
  • 1
  • 18
  • 36
  • I've given you an upvote, but please correct these misspells in frirst fragment. Should be `` – gonczor May 30 '17 at 09:36
  • This might sound stupid but how come this doesn't work? `split = re.findall('

    .+?

    ',content)` ...... `second = split[1]`......it gives me an `index out of range` error. How do I get the 2nd element?
    – Zorgan May 30 '17 at 10:01
  • @Zorgan, could you have changed the content? it works in my console: `>>> split = re.findall('

    (.+?)

    ',string) >>> split[1] 'And also this string'`
    – Isdj May 30 '17 at 10:21
  • Yeah it was my content, i've fixed it now. Thanks. – Zorgan May 30 '17 at 11:01
13

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']
Rich Rajah
  • 2,256
  • 1
  • 12
  • 14
0

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']
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49