[Perth-pm] Using \L and \E inside a variable, inside a substitution s///
Peter Hallam
perth.pm at inatick.com
Sun Oct 24 16:58:59 PDT 2010
On Mon, 25 Oct, 00:39 +0800 James Bromberger wrote:
> 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/;
Hi James,
Given $url and $search I came up with:
print $url if $url =~ s|$search|lc($1)."/$2"|e;
However, I'd be tempted to capture the forward slash to with:
my $search = '^([^:]+://[^/]+)(/?.*)?$';
Which then alleviates the need to use the pipe character in the substitution:
print $url if $url =~ s/$search/lc($1).$2/e;
Regards,
Peter.
More information about the Perth-pm
mailing list