0

I want to match content inside every font tag with a color:

inside html <font color="#000000">this should be matched</font><font color="#777777">this shouldn't be</font><font color="#000000">this should be matched too</font> inside html

But when I use the following regex:

<font color=\"#000000\">(.*)<\/font>

It will match the whole string starting with first font tag, including the one that has #77777 color. Can someone please tell me what am I doing wrong?

Thanks

all jazz
  • 2,007
  • 2
  • 21
  • 37

4 Answers4

1

(.*) matches everything, including other elements. It's in a greedy mode, meaning it will try to consume as much as possible. You can add a non-greedy operator to the '*' star operator, usually using .?*

Try with :

<font color=\"#000000\">(.*?)<\/font>
Scharron
  • 17,233
  • 6
  • 44
  • 63
0

You should add a 'non-greedy operator', i.e.:

<font color=\"#000000\">(.*?)<\/font>
orique
  • 1,295
  • 1
  • 27
  • 36
0

The <font> and <basefont> tags should be avoided because they have been deprecated since HTML version 4. Fonts should be provided using CSS rather than the <font> tag.

0

Try this...

<font color=\"#000000\">([^<>]+)<\/font>
Krishna
  • 381
  • 2
  • 9