Solved

Pattern Matching

Posted on 2002-07-01
5
285 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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

713 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