[VPM] regular-expressions and variables

Darren Duncan darren at DarrenDuncan.net
Sun Nov 12 20:03:19 PST 2006


At 8:38 AM -0800 11/11/06, Jer A wrote:
>hello all perl gurus,
>
>I have another problem - a regular expression problem
>
>how do i use scalar variables in substitution and complex matching?
>
>eg I want the following to work.
>
>$string =~ s/^$variable//;
>
>$string =~ m/^([^$variable]*)/;
>
>thanks in advance for your help.
>
>-Jeremy A.

As far as I can tell, your examples already work.  Observe:

     Last login: Sun Nov 12 17:12:50 on ttyp1
     Welcome to Darwin!
     darren-duncans-power-mac-g4:~ darrenduncan$ perl
     my $foo = 'abc';
     my $bar = 'ab';
     $foo =~ s/^$bar//;
     print "foo is '$foo'\n";
     foo is 'c'
     darren-duncans-power-mac-g4:~ darrenduncan$ perl
     my $foo = 'abc';
     my $bar = 'b';
     $foo =~ m/^([^$bar]*)/;
     print "have $foo, matched is '$1'\n";
     have abc, matched is 'a'
     darren-duncans-power-mac-g4:~ darrenduncan$

That example is running under Perl 5.8.6, which is fairly recent.

What problem are you having?

-- Darren Duncan


More information about the Victoria-pm mailing list