Here we have used regex with grep method.Pattern for matching the tags /<p>(.+?)</p>/
For example i have used for p tag
text="<p>He's the man who helped make \"Slumdog Millionaire\" an international hit, scoring the soundtrack of the Oscar winning film. Despite his performance at the Oscars ceremony and being caught up in all the glitz and adulation, Rahman is a reluctant star.</p>\r\n<p>He's worked on films since he was a teenager, taking over the role of family breadwinner after his father died and followed in his footsteps as a composer.</p>\r\n<p>While he had stints writing advertising jingles in India, composing for films as been his life's work so far, yet from his studio in Chennai he admitted to CNN he didn't want to score films.</p>\r\n<h3>El pasaje estándar Lorem Ipsum, usado desde el año 1500.</h3>\r\n"
Fetch the first p tag
text.grep(/<p>(.+?)<\/p>/).first
"<p>He's the man who helped make \"Slumdog Millionaire\" an international hit, scoring the soundtrack of the Oscar winning film. Despite his performance at the Oscars ceremony and being caught up in all the glitz and adulation, Rahman is a reluctant star.</p>\r\n"
Fetch the last p tag
text.grep(/<p>(.+?)<\/p>/).last
"<p>While he had stints writing advertising jingles in India, composing for films as been his life's work so far, yet from his studio in Chennai he admitted to CNN he didn't want to score films.</p>\r\n"
No comments :
Post a Comment