Home: Perl Programming Help: Regular Expressions:
replacement with backreference

New User

Aug 7, 2007, 9:08 PM

Views: 10348
replacement with backreference

I have a number of regex that work fine but I decided to move them into a file like:

"my_pattern" "my_replacement"

This works fine until I use a backreference like:

"I_love_(perl)" "no_so_much_$1_now :)"

In fact the backreference is seen as a $1 and doesn't do its job.
As FYI I parse this file and at some point I have for example:

my $pattern=$field[0]; 
my $replacement=$field[1];
my $newtext=~ s|$pattern|$replacement|;

What am I missing, here?

Thanks for any tips.


Aug 8, 2007, 7:39 PM

Views: 10337
Re: [ericm] replacement with backreference

I may be missing what you are trying to do here.

my $pattern = $field[0];  
my $replacement = $field[1];

my $test = $field[0];

if ($test =~ /^$pattern$/) {

# $& holds the entire regex match.
my $test1 =~ s/$&/$replacement/;

# Or you could use $1 if you would like.
my $test2 =~ s/$1/$replacement/;

print $test1, "\n";
print $test2, "\n";

} else {
print "Did not find a match for $pattern in $test";

(This post was edited by eWish on Aug 8, 2007, 7:46 PM)

New User

Aug 8, 2007, 8:31 PM

Views: 10332
Re: [eWish] replacement with backreference

let's say I have a file that I want to format differently.

for example I have:

but I want easily to change the format to:
and later
address, phone, name

so in order to do that I created only 1 method that will reorder the file.
this method takes 2 arguments pattern and replacement.

I put in a file my pattern and replacement like:

(\w+):(\w+):(\w+)|\1 --\2-- \3
(\w+):(\w+):(\w+)|\3, \2, \1

at some point this file is parsed

my $text="name:address:phone"; 
my $pattern=$field[0];
my $replacement=$field[1];

$text =~ s/$pattern/$replacement/;
print $text;
# actual display: \1 --\2-- \3
# expected : name--phone--address

so a work around I found is to match first and then reuse the backreferences like:

my $test =~ m/^$pattern/;
$test =~ s/$pattern/$1--$2--$3/;
#display as expected

but in the last example I am loosing what I am trying to achieve which is to move out the code that change.


Aug 8, 2007, 10:11 PM

Views: 10326
Re: [ericm] replacement with backreference

To me this would be the way to go.

my $string = 'Name:Address:Phone';  
my $pattern = ':';
my $replacement = '--';
$string =~ s/$pattern/$replacement/g;

or you could also do

my $string = 'Name:Address:Phone';   
print join('--', split(/:/, $string));

If you are not wanting something so trivial as a replacement of -- or :: etc... and you really need to catch a particular pattern then I am not sure how it will work with the backreferences in a variable like $replacement.

The backreference's seem to be lost in translation, I tested some code and $1, $2 and $3 never contained anything when doing:

my $replacement = "$1--$2--$3";

You could do:

my $string = 'Name:Address:Phone'; 
my $pattern = '(\w+):(\w+):(\w+)';
my $replacement = '--';

my $new_string = $string;
$new_string =~ s/$pattern/$1$replacement$2$replacement$3/;

print $new_string;

(This post was edited by eWish on Aug 8, 2007, 10:40 PM)


Aug 9, 2007, 12:08 AM

Views: 10319
Re: [ericm] replacement with backreference

I think you want to use the qr// operator. The qr// operator allows you to store patterns for regexps in variables.