Perl/Regex question (hopefully simple)

Devin Whalen devin-Gq53QDLGkWIleAitJ8REmdBPR1lH4CV8 at public.gmane.org
Tue Jun 29 16:51:59 UTC 2004


On Tue, 2004-06-29 at 12:33, Madison Kelly wrote:
> Devin Whalen wrote:
> > On Tue, 2004-06-29 at 11:56, Madison Kelly wrote:
> > 
> >>Hi all,
> >>
> >>   I'm not having a lot of luck googling for the answer to this 
> >>question. I hope someone can help or point me in the right direction 
> >>(like what perldoc to read).
> >>
> >>   I am trying to do a regex substitute where I need to maintain two 
> >>pieces of dynamic data. Specifically I am trying to convert some keys 
> >>into a proper html link. For example, I want to convert:
> >>
> >>[url=groups.google.ca]Google Groups[/url]
> >>
> >>into
> >>
> >><a href="groups.google.ca" target="_new">Google Groups</a>
> >>
> >>I was trying something like this but it isn't working...
> >>
> >>$help_body =~ s/\[url=(.*)\](.*)\[\/url\]/(<a href=").*(" 
> >>target="_new">).*(<\/a>)/gi;
> >>
> >>So how do I go about substituting values around variable information 
> >>that I want to save?
> >>
> >>Thanks!!
> >>
> >>Madison
> >>--
> >>The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
> >>TLUG requests: Linux topics, No HTML, wrap text below 80 columns
> >>How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml
> > 
> > 
> > Dam, sorry to do this but I forget my second set of ()
> > so change it to:
> > $url=~s/\[url=(.*?)\](.*)\[\/url\]/<a href="$1" target="_new">$2<\/a>/g;
> > 
> > I knew I shouldn't have rushed and sent the code :).
> > 
> > Later
> > 
> > 
> 
> Thank you Devin and Phillip!!
> 
> As is often the (frustrating) case just after sending the email I found 
> the answer... Here is what I did:
> 
> $help_body =~ s/\[url=(.*)\](.*)\[\/url\]/<a href="\1" 
> target="_new">\2<\/a>/gi;
> 
> Which seems very much like what you both recommended. Do you know if one 
> method is better or worse than the other or are they just two ways to do 
> the same thing?
> 
> Thank you both very much!!
> 
> Madison
> --
> The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
> TLUG requests: Linux topics, No HTML, wrap text below 80 columns
> How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml


$help_body =~ s/\[url=(.*)\](.*)\[\/url\]/<a href="\1" 
> target="_new">\2<\/a>/gi;

you really should add a ? to your (.*)...both of them.  This makes the
match not greedy.  I am sure the way you do it will work but there could
be that one time that you need the non-greedy match (I forgot the second
? in mine but Phillip didn't).  For example try running this through the
one without the ?

my $url="[url=groups.google.ca]Google [Groups][/url]";

This is just being extra careful but better safe than sorry :). 
Although on second look, I don't think you would (or could) have a ] in
either a url or the name of the url.  But non-greedy may be faster.  I
am not really a perl expert so don't quote me ;).  Hope this helps.

Later




-- 
Devin Whalen
Programmer
Synaptic Vision Inc
Phone-(416) 539-0801
Fax- (416) 539-8280
1179A King St. West
Toronto, Ontario
Suite 309 M6K 3C5
Home-(416) 653-3982
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list