Reading Time: 1 minutes
Extract contents of a Paragraph tag with a given id in Python
Extract contents of a Paragraph tag with id 'para': Write a Regular Expression in Python to extract the contents lying between a paragraph tag with an id 'para'.
Extract contents of a Paragraph tag with id 'para'
import re sourceString = ''' <p id = 'para'>Contents of paragraph</p> ''' matchObject = re.search('<p id\s*=\s*\'para\'>(.*?)</p>', sourceString) print(matchObject.group(1)) # 'Contents of paragraph'
To learn more about Regular Expressions in Python, click here.