Solved

Pattern Matching

Posted on 2002-07-01
5
287 Views
Last Modified: 2010-03-05
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
0
Comment
Question by:lakshmisubram
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 7123367
Which of the three "color"s above should be replaced, and what should they be replaced with?
0
 
LVL 84

Accepted Solution

by:
ozo earned 30 total points
ID: 7123807
$line =~ s/\b\Q$match_str\E\b|(<[^<>]*>)/$1||qq<<font color="#ff0060">$match_str<\/FONT>>/egi;
0
 

Author Comment

by:lakshmisubram
ID: 7124028
Thank you so much !! It's working fine .
Can you please explain your regex in words ? ( Since I am novice in regex )
0
 
LVL 84

Expert Comment

by:ozo
ID: 7127824
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>
0
 

Author Comment

by:lakshmisubram
ID: 7130128
Thanks you so much !!It's more clear to me now .
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question