• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help in Regular Expression.

 
Ranch Hand
Posts: 419
Mac jQuery Objective C
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an html string. I want to remove style tag and content under this tag as well. Can any body help me what should be the regex for this?
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Andrey, keep in mind that your regular expression will replace everything between the starting tag of the first style element, and the ending tag of the last style element. Your quantifier should probably be reluctant.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are more problems here.

One is minor - what if the style tag has attributes? (Which is the point of a style tag, after all.) You probably want to match "<style" or possibly "><style.*?>" rather than "<style>".

Another problem is major - what if the <style> tag is nested within other <style> tags? How does the regex determine which tag matches which? It's possible to write a mildly complex expression that handles one level of nesting. Or a more complex expression that handles up to two levels of nesting. Or a much more complex expression that handles up to three levels. But this will be really ugly at this point. It is fundamentally impossible to write a regex that handles an arbitrary (infinite) number of levels.

Another issue is comments - what if there are some tags that have been commented out? How does the regex know to ignore them?

For these last two reasons, and probably others, it's almost always a bad, bad idea to try to parse HTML with a regex. HTML is more complicated than many people realize. However there are many good HTML parsers out there - that do not rely on using regular expressions. Use a proven HTML parser instead.

 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The classic version of this answer is here.
 
reply
    Bookmark Topic Watch Topic
  • New Topic