Link to home
Start Free TrialLog in
Avatar of lakshmisubram
lakshmisubram

asked on

Pattern Matching

Consider the following variables
$line = "<FONT color=\"#ff0060\">Color</FONT> printers are laser color printers";
$match_str = "color";

I have to match and replace $match_str in $line with <FONT color="#ff0060">$match_str</FONT>. But 'color' inside <FONT> tag should not be matched and replaced .


My output should be
<FONT color="#ff0060">Color</FONT> printers are laser <FONT color="#ff0060">color</FONT> printers .

I am searching for 'color' ($match_str) in the given $line to highlight that word in red color . The pattern should not match 'color' inside the FONT tag . But it should match and highlight the word 'color' in the line other than the one inside FONT .

How to search this pattern match and replace ?

Thanks for the help.
Lakshmi
Avatar of ozo
ozo
Flag of United States of America image

Which of the three "color"s above should be replaced, and what should they be replaced with?
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lakshmisubram
lakshmisubram

ASKER

Thank you so much !! It's working fine .
Can you please explain your regex in words ? ( Since I am novice in regex )
b\Q$match_str\E\b matches the word "color"
(<[^<>]*>) matches a string in <> and saves it in $1
$1||qq<<font color="#ff0060">$match_str<\/FONT>> puts the $1 back, or inserts <FONT color="#ff0060">Color</FONT>
Thanks you so much !!It's more clear to me now .