Extract text between tags using regex python

Viewed 85

I have text below

<p>FIFA is a non-profit organization which describes itself as an international governing body of association football, fútsal and beach soccer. It is the highest governing body of football.</p>\\n\\n<p><strong>Description:</strong><br />\\nFIFA was founded in 1904[3] to oversee international competition among the national associations of Belgium, Denmark, France, Germany, the Netherlands, Spain, Sweden, and Switzerland. Headquartered in Zürich, its membership now comprises 211 national associations. These national associations must each also be members of one of the six regional confederations into which the world is divided: Africa, Asia, Europe, North & Central America and the Caribbean, Oceania, and South America.</p>\\n\\n<p><strong>Motto</strong><strong> </strong><br />\\n For the Game. For the World.
</p>

I need to extract the text for first paragraph

and

I tried using <p>(.*)<\/p> i got till last </p

module i m using is re

import re

4 Answers

Try this
Using only raw regex

>>> import re
>>> t = '<p>your string</p>'
>>> re.findall(r'>(.+?)<', t)
['your string']


>>> import re
>>> t = '<b>using b tag</b>'
>>> re.findall(r'>(.+?)<', t)
['using b tag']


>>> import re
>>> t = '<p>1st p</p><p>2nd p</p>'
>>> r = re.compile('<p>(.+?)</p>')
>>> r.findall(t)[0]
'1st p'

hope it helps with your problem.

You can parse html with BeautifulSoup:

import bs4
string = "<p>FIFA is a non-profit organization which describes itself as an international governing body of association football, fútsal and beach soccer. It is the highest governing body of football.</p>\\n\\n<p><strong>Description:</strong><br />\\nFIFA was founded in 1904[3] to oversee international competition among the national associations of Belgium, Denmark, France, Germany, the Netherlands, Spain, Sweden, and Switzerland. Headquartered in Zürich, its membership now comprises 211 national associations. These national associations must each also be members of one of the six regional confederations into which the world is divided: Africa, Asia, Europe, North & Central America and the Caribbean, Oceania, and South America.</p>\\n\\n<p><strong>Motto</strong><strong> </strong><br />\\n For the Game. For the World.</p>"

soup = bs4.BeautifulSoup(string)
soup.find("p").text  # Get the text inside the first p tag
# 'FIFA is a non-profit organization which describes itself as an international governing body of association football, fútsal and beach soccer. It is the highest governing body of football.'

A non regex solution for the first paragraph:

s = "<p>FIFA is a non-profit organization which describes itself as an international governing body of association football, fútsal and beach soccer. It is the highest governing body of football.</p>\\n\\n<p><strong>Description:</strong><br />\\nFIFA was founded in 1904[3] to oversee international competition among the national associations of Belgium, Denmark, France, Germany, the Netherlands, Spain, Sweden, and Switzerland. Headquartered in Zürich, its membership now comprises 211 national associations. These national associations must each also be members of one of the six regional confederations into which the world is divided: Africa, Asia, Europe, North & Central America and the Caribbean, Oceania, and South America.</p>\\n\\n<p><strong>Motto</strong><strong> </strong><br />\\n For the Game. For the World.</p>"

print (s.split("<p>")[1].split("</p>")[0])

Output:

FIFA is a non-profit organization which describes itself as an international governing body of association football, fútsal and beach soccer. It is the highest governing body of football.

The same thing with a regex:

print (re.split("<p>|</p>",s)[1])

<p>(.*)<\/p> by default the * operator is greedy what you want is to make it non-greedy if you do this it should work <p>(.*?)</p>.
Also since it looks like you are trying to parse html files try looking into Beautiful Soup.

Related