CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



ericm
New User

Aug 7, 2007, 9:08 PM

Post #1 of 5 (4293 views)
replacement with backreference Can't Post

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:

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


What am I missing, here?

Thanks for any tips.


eWish
User

Aug 8, 2007, 7:39 PM

Post #2 of 5 (4282 views)
Re: [ericm] replacement with backreference [In reply to] Can't Post

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


Code
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)


ericm
New User

Aug 8, 2007, 8:31 PM

Post #3 of 5 (4277 views)
Re: [eWish] replacement with backreference [In reply to] Can't Post

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

for example I have:
name:address:phone

but I want easily to change the format to:
name--phone--address
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


Code
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:


Code
 
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.


eWish
User

Aug 8, 2007, 10:11 PM

Post #4 of 5 (4271 views)
Re: [ericm] replacement with backreference [In reply to] Can't Post

To me this would be the way to go.

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


or you could also do


Code
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";

Edit:
You could do:

Code
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)


KevinR
Veteran


Aug 9, 2007, 12:08 AM

Post #5 of 5 (4264 views)
Re: [ericm] replacement with backreference [In reply to] Can't Post

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

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives