Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > How to use backreferences in a variable for a regular expression

Reply
Thread Tools

How to use backreferences in a variable for a regular expression

 
 
Mark Fletcher
Guest
Posts: n/a
 
      05-18-2004
I have tried everything I can think of and cannot solve this. Any help appreciated.

#!/usr/bin/perl -w
use strict;

my ($s, $pat, $rpl);

$s = "ABCDEF abc000 RSTUV";
$pat = '(.*abc)...(.*)';
$rpl = '${1}777${2}';

# Works...
$_ = $s;
s/(.*abc)...(.*)/${1}777${2}/;
print "$_\n";

# Does not work...
$_ = $s;
s/$pat/$rpl/;
print "$_\n";

OUTPUT:
ABCDEF abc777 RSTUV
${1}777${2}
 
Reply With Quote
 
 
 
 
Mark Fletcher
Guest
Posts: n/a
 
      05-19-2004
s/$pat/eval qq("$rpl")/e;
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use variable in regular expression CarpeSkium@gmail.com Python 5 08-02-2007 01:32 PM
Set a variable from a substring of another variable using Regular Expression Tony Perl Misc 2 04-20-2005 11:57 PM
regular expression backreferences Marshall Dudley Perl Misc 4 02-15-2005 08:22 PM
regular expressions - math on backreferences Chris Nolte Perl 9 05-25-2004 07:43 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57