dynamic regex
am 24.08.2007 17:24:22 von Thomas Peter
hi,
i want to parametrize the usage of the search and the replacement of
s/search/replace/
the following code:
use Getopt::Long;
GetOptions("p=s" => \$args{pattern}, "r=s" => \$args{replacement});
print s/$args{pattern}/$args{replacement}/;
(yes i could achieve that with perl -p -e but that's only a small
snippet from a bigger context)
when i call the thing with -p '.*(\d+).*' and -r '\1'
it only prints \1 not the first occurence of \d+
any suggestions what's wrong?
thanx and have a nice weekend!
thomas
Re: dynamic regex
am 24.08.2007 18:37:08 von Paul Lalli
On Aug 24, 11:24 am, Thomas Peter
group.org> wrote:
> hi,
> i want to parametrize the usage of the search and the replacement of
> s/search/replace/
>
> the following code:
>
> use Getopt::Long;
> GetOptions("p=s" => \$args{pattern}, "r=s" => \$args{replacement});
> print s/$args{pattern}/$args{replacement}/;
You know that s/// does not return the modified string, right? You're
printing the number of substitutions that will be made (which will be
either 0 or 1, since you didn't use the /g modifier)
>
> (yes i could achieve that with perl -p -e but that's only a small
> snippet from a bigger context)
>
> when i call the thing with -p '.*(\d+).*' and -r '\1'
> it only prints \1 not the first occurence of \d+
>
> any suggestions what's wrong?
You need to eval your replacement, twice. change s/search/replace/ to
s/search/replace/ee
Paul Lalli
Re: dynamic regex
am 24.08.2007 18:56:28 von papahuhn
Paul Lalli schrieb:
> On Aug 24, 11:24 am, Thomas Peter
>> when i call the thing with -p '.*(\d+).*' and -r '\1'
>> it only prints \1 not the first occurence of \d+
>> any suggestions what's wrong?
>
> You need to eval your replacement, twice. change s/search/replace/ to
> s/search/replace/ee
>
> Paul Lalli
Besides,\1 is only valid in the matching part of an s///. Probably you
need $1.
Re: dynamic regex
am 24.08.2007 19:01:49 von Paul Lalli
On Aug 24, 12:56 pm, papahuhn wrote:
> Paul Lalli schrieb:
>
> > On Aug 24, 11:24 am, Thomas Peter
> >> when i call the thing with -p '.*(\d+).*' and -r '\1'
> >> it only prints \1 not the first occurence of \d+
> >> any suggestions what's wrong?
> Besides,\1 is only valid in the matching part of an s///.
> Probably you need $1.
It's perfectly *valid*. It's just not recommended:
$ perl -wle'
$s = "foo bar";
$s =~ s/(foo)/\1BAR/;
print $s;
'
\1 better written as $1 at -e line 3.
fooBAR bar
Paul Lalli
Re: dynamic regex
am 24.08.2007 19:08:35 von Glenn Jackman
At 2007-08-24 12:37PM, "Paul Lalli" wrote:
> On Aug 24, 11:24 am, Thomas Peter
> group.org> wrote:
> > i want to parametrize the usage of the search and the replacement of
> > s/search/replace/
> >
> > the following code:
> >
> > use Getopt::Long;
> > GetOptions("p=s" => \$args{pattern}, "r=s" => \$args{replacement});
> > print s/$args{pattern}/$args{replacement}/;
[...]
> > when i call the thing with -p '.*(\d+).*' and -r '\1'
> > it only prints \1 not the first occurence of \d+
>
> You need to eval your replacement, twice. change s/search/replace/ to
> s/search/replace/ee
And you want to use '$1', not '\1', in the replacement part. \1 is only
used within a regular expression.
Also note that in /.*(\d+).*/ the first ".*" is greedy, so you'll only
get the *last* digit. Try /.*?(\d+).*/
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Re: dynamic regex
am 24.08.2007 19:16:52 von Glenn Jackman
At 2007-08-24 01:01PM, "Paul Lalli" wrote:
> On Aug 24, 12:56 pm, papahuhn wrote:
> > Paul Lalli schrieb:
> >
> > > On Aug 24, 11:24 am, Thomas Peter
> > >> when i call the thing with -p '.*(\d+).*' and -r '\1'
> > >> it only prints \1 not the first occurence of \d+
> > >> any suggestions what's wrong?
>
> > Besides,\1 is only valid in the matching part of an s///.
> > Probably you need $1.
>
> It's perfectly *valid*. It's just not recommended:
>
> $ perl -wle'
> $s = "foo bar";
> $s =~ s/(foo)/\1BAR/;
> print $s;
> '
> \1 better written as $1 at -e line 3.
> fooBAR bar
Is this why it's not recommended?
$ perl -le '$_="abc1234def";print;$rep=q{\1};s/.*?(\d+).*/$rep/ee;print '
abc1234def
SCALAR(0x124c74)
$ perl -le '$_="abc1234def";print;$rep=q{$1};s/.*?(\d+).*/$rep/ee;print '
abc1234def
1234
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Re: dynamic regex
am 24.08.2007 19:17:49 von Glenn Jackman
At 2007-08-24 01:16PM, "Glenn Jackman" wrote:
> At 2007-08-24 01:01PM, "Paul Lalli" wrote:
> > On Aug 24, 12:56 pm, papahuhn wrote:
> > > Besides,\1 is only valid in the matching part of an s///.
> > > Probably you need $1.
> >
> > It's perfectly *valid*. It's just not recommended:
> >
> > $ perl -wle'
> > $s = "foo bar";
> > $s =~ s/(foo)/\1BAR/;
> > print $s;
> > '
> > \1 better written as $1 at -e line 3.
> > fooBAR bar
>
> Is this why it's not recommended?
>
> $ perl -le '$_="abc1234def";print;$rep=q{\1};s/.*?(\d+).*/$rep/ee;print '
> abc1234def
> SCALAR(0x124c74)
>
> $ perl -le '$_="abc1234def";print;$rep=q{$1};s/.*?(\d+).*/$rep/ee;print '
> abc1234def
> 1234
btw, that's perl v5.6.1
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Re: dynamic regex
am 25.08.2007 13:21:21 von Dummy
Paul Lalli wrote:
> On Aug 24, 12:56 pm, papahuhn wrote:
>> Paul Lalli schrieb:
>>
>>> On Aug 24, 11:24 am, Thomas Peter
>>>> when i call the thing with -p '.*(\d+).*' and -r '\1'
>>>> it only prints \1 not the first occurence of \d+
>>>> any suggestions what's wrong?
>
>> Besides,\1 is only valid in the matching part of an s///.
>> Probably you need $1.
>
> It's perfectly *valid*. It's just not recommended:
Because in a double quoted string \ is usually interpreted as an octal
escape. Is "\1" the SOH character or a back-reference to (\d+)?
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall