Solved

Pattern Matching

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now