lakshmisubram
asked on
Pattern Matching
Consider the following variables
$line = "<FONT color=\"#ff0060\">Color</F ONT> 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</FON T> printers are laser <FONT color="#ff0060">color</FON T> 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
$line = "<FONT color=\"#ff0060\">Color</F
$match_str = "color";
I have to match and replace $match_str in $line with <FONT color="#ff0060">$match_str
My output should be
<FONT color="#ff0060">Color</FON
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
Which of the three "color"s above should be replaced, and what should they be replaced with?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much !! It's working fine .
Can you please explain your regex in words ? ( Since I am novice in regex )
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</FON T>
(<[^<>]*>) matches a string in <> and saves it in $1
$1||qq<<font color="#ff0060">$match_str
ASKER
Thanks you so much !!It's more clear to me now .