DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Strip Html Tags And Fetching The P Tags

05.23.2009
| 2943 views |
  • submit to reddit
        The regex below removes html tags from string.

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"

text.grep(/<p>(.+?)<\/p>/)
To fetch the first p tag
text.grep(/<p>(.+?)<\/p>/).first
To fetch the last p tag
text.grep(/<p>(.+?)<\/p>/).last