-2

I have string which contains HTML tags. In the head tag of the HTML, the user has a defined style and also for the body of the html tag.

How do I get the style value using a regular expression?

A string is like:

>   String text = "<html><head style="colour:red;"></head><body
> style="padding:9px;">Sagar hi</body></html>";
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sagar
  • 272
  • 1
  • 4
  • 13
  • Why not use a HTML parser instead? – juharr Jul 13 '16 at 17:46
  • @juharr can i use it in windows application ? – Sagar Jul 13 '16 at 17:48
  • 5
    Please read [this famous question](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags?rq=1), and then consider including a parsing library. – 4castle Jul 13 '16 at 17:50
  • @Sagar, Sure. You can just grab the [HTML Utility Pack](https://htmlagilitypack.codeplex.com/) – juharr Jul 13 '16 at 17:55
  • Obligatory reading: *[RegEx match open tags except XHTML self-contained tags](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454)* – Peter Mortensen Jul 22 '16 at 20:54

1 Answers1

0

Yeah, how about /style=["]([^"]+)["]/ which returns the desired information in $1.

Problem arising is if there are other tags that sport a style sub-tag, so to speak. The style that I cite in the regular expression above presupposes that a has been opened but that the corresponding has not yet been collected. You could detect these conditions, and fire off the action for the regex only under that condition, if you coded in LEX. That's my new recommendation.

Whichever way the wind blows . . .

"A people without a history is like the wind on the buffalo grass." Sitting Bull