[Perth-pm] Using \L and \E inside a variable, inside a substitution s///

Neil Hunt neil.hunt at huntcorp.com.au
Sun Oct 24 16:54:14 PDT 2010


Hi James,
You can pull out the match (i.e. $1) and pass it through a tr, which should
achieve your goal.

Something like the following does what you're asking:


my $url = "http://www.FOo.COm/wibbLE";
$url =~ m|(\w+)://([^/:]+)(:\d+)?/(.*)|;
my $protocol = $1;
my $domainName = $2;
$uri= $4;
$domainName=~tr/A-Z/a-z/;

Note that a ' does not cause it's contents to be interpolated, you need to
use ".

Neil


2010/10/25 James Bromberger <james at rcpt.to>

>  Hi all,
>
> I'm trying to to a substitution on a URL to lower-case the the protocol and
> host name in one regex, using variables for either side of the substitution:
> my $url = 'http://www.FOo.COm/wibbLE';
> my $search = '^([^:]+://[^/]+)/?(.*)?$';
> my $replace = '\L$1\E/$2';
> print $url if $url =~ /$search/$replace/;
>
> Sadly, the result is the literal "*\L$1\E/$2*".
>
> Limitation is that I don't want to use eval() (also known as /e, and /ee),
> as I don't trust the content of $replace - fine if it doesn't nicely do a
> replace, bad if the eval() does a call to unlink and friends. (Of course,
> with using eval the $replace could contain '*lc($1) . "/$2"*'.)
>
> Any ideas?
>   James
>
> --
> *James Bromberger*
> Aus Mobile: +61 422 166 708
> Email: james *_AT_* rcpt.to, Web: www.james.rcpt.to
> MSN: james*_AT_*rcpt.to, AIM: JamesEBromberger, Skype: james.bromberger (*
> _AT_* -> @)
>
> _______________________________________________
> Perth-pm mailing list
> Perth-pm at pm.org
> http://mail.pm.org/mailman/listinfo/perth-pm
>



-- 
Neil Hunt
Senior Consultant
HuntCorp Enterprises
Phone: 0412 474 140
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/perth-pm/attachments/20101025/c4761100/attachment.html>


More information about the Perth-pm mailing list