Perl/Regex question (hopefully simple)
Devin Whalen
devin-Gq53QDLGkWIleAitJ8REmdBPR1lH4CV8 at public.gmane.org
Tue Jun 29 16:07:44 UTC 2004
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
I am kinda busy so I am not sure if this is perfect but it works for the
example you gave. You should really test it and modify it to fit your
need because again, I didn't really test it extensively.
#!/usr/bin/perl -w
use strict;
my $url="[url=groups.google.ca]Google Groups[/url]";
$url=~s/\[url=(.*?)\].*\[\/url\]/<a href="$1" target="_new">$2<\/a>/g;
warn "$url\n";
If you have any questions, just ask :)
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