Regex Question
Peter Scott
Peter at PSDT.com
Wed Jul 17 12:42:01 CDT 2002
>At 09:35 AM 7/17/02 -0700, abez wrote:
>
>>I'm having problems making a regex which does this
>>
>>finds all
>>
>>$varname
>>${varname}
>>but doesn't find
>>$varname{
>>
>> my @all = ($k =~ /(\$[a-zA-Z_0-9]+)/g);
>> my @all2 = ($k =~ m#(\$\{[a-zA-Z_0-9]+\})#g);
>>
>>I've tried ($k =~ /(\$[a-zA-Z_0-9]+)(?!\{)/g);
>>but that doesn't work..
$ cat /tmp/foo
#!/usr/bin/perl -l
use strict;
use warnings;
{
my %hashed;
my @syms = ('A' .. 'Z', 'a' .. 'z');
sub randvar {
my $v = shift;
$hashed{$v} || ($hashed{$v} = join '' => map $syms[rand @syms] =>
1 .. 1+rand 10);
}
}
$_ = '$one ${two} $three{four} ${two} $one';
s/ (?<= \$ ) ( (\{)? \w+\b (?(2) \} ) (?! \{ ) ) /randvar($1)/gxe;
print;
$ /tmp/foo
$J $qSq $three{four} $qSq $J
$ /tmp/foo
$zDo $oaKs $three{four} $oaKs $zDo
--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com/
More information about the Victoria-pm
mailing list