Explanation:
($new_String = $old_string) =~ s/(href=.*?)(>)/$1 target="_blank"$2/gims;

($new_String = $old_string)
This sets the $new string to what is equal to the other side of =~,
it just eliminates the need for a temp variable so you get to keep 
the original.

s/(href=.*?)(>)/$1 target="_blank"$2/gims;
s/// is the substitution command
the ( ) in the left side of s/// is to store the $1 and $2 vars of the
search through $old_string. Then on the right it puts what it found
back in and puts what you want {target="_blank"} in between
them {$1 target="_blank"$2}.

/(href=.*?)(>)/
First, it looks for href= and everything behind it up to the next >.
.* is a wild card to grab anything (including white space like new 
lines {\n}, tabs {\t}, etc.), the ? behind it makes it non-greedy.  So 
(href=.*?) will find everything from href= until it reaches > and 
stops (before the >).  The reason I did it this way was so you can 
have multiple href's in the entire string.  If not, the regex would snag 
everything from the first href to the last > or the string.  Then you 
need to have the > to follow your insertion of target="_blank".

gism
These are modifiers to the s///.  g is for global replacement, i is for
case insensitive, m treats the string as multiple lines (probably not
needed, but just put in there to be complete), s is to treat your 
string as a single line no matter how many new lines are in it.

An excellent book on regex's is Mastering Regular Expressions by
Orielly.

Shawn

----- Original Message ----- 
From: "Henry Potter" <[EMAIL PROTECTED]>
To: "Shawn" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Monday, December 04, 2000 9:25 PM
Subject: Re: Help on regular expression


> Thanks Shawn! it worked!
> btw do you mind explaining how the regular expression functions?

_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/activeperl

Reply via email to