?
Solved

Pattern Matching

Posted on 2002-07-01
5
Medium Priority
?
289 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 120 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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

719 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