[email protected]
[Top] [All Lists]

Re: Regexp substitution

Subject: Re: Regexp substitution
From: "John W. Kennedy"
Date: Sat, 15 Jul 2006 10:08:16 -0400
pDale wrote:
> On 7/15/06, [email protected] <[email protected]> wrote:
>> hi pDale --
>> In a message dated 7/15/2006 12:35:22 A.M. Eastern Standard Time,
>> [email protected] writes:
>>> I'm having a hard time "asking the question" to Google...
>>> I want to read regular expressions from a config file to use in
>>> substitions, but I can't get the regexp to work for memorized
>>> groupings.
>> try using eval(), e.g.:
>> C:\@Work\Perl>perl -we "use strict;
>> my $p1 = '^([^0-9]*)([0-9].*)$'; my $p2 = '$2 $1'; my $str = 'abc123xyz';
>> eval qq(\$str =~ s/$p1/$p2/); print $str"
>> 123xyz abc
> Yes, eval does the trick.
> My thanks to Bill and $Bill.
> Maybe I *will* be able to get to sleep tonight!

But you can do it without using eval:

use strict;
use warnings;
my $p1 = '^([^0-9]*)([0-9].*)$';
my $p2 = '"'.'$2 $1'.'"'; #Note wrapping it with double quotes
my $str = 'abc123xyz';
$str =~ s/$p1/$p2/ee; #Note double e
print $str, "\n";

John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
   -- Charles Williams.  "Taliessin through Logres: Prelude"

ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

<Prev in Thread] Current Thread [Next in Thread>