Home: Perl Programming Help: Regular Expressions:
how do I use $1 in a $var for subsitutions



cynick
New User

May 19, 2009, 9:15 PM


Views: 20487
how do I use $1 in a $var for subsitutions

It would be better with example:

For example this works:
$d = "hello 123 world 456!";
$b = "hello";
$c = "bye";

$d =~ s/$b/$c/;


print "$d\n";

=> produces "bye 123 world 456!"

How do I produce:
"bye world 456 123"?

Putting $1 or $2 inside $c doesn't work, I get "$1" and "$2" instead of the actual match..


vikas.deep
User

May 19, 2009, 9:31 PM


Views: 20480
Re: [cynick] how do I use $1 in a $var for subsitutions

Check -out the following

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

my $d = "hello 123 world 456!";
my $b = "hello";
my $c = "bye";

$d =~ s/^hello 123 (world 456)/$c $1 123/;

print "$d\n";

The out put is

Code
[root@localhost Documents]# perl prog.pl 
bye world 456 123!

-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"

(This post was edited by vikas.deep on May 19, 2009, 9:32 PM)


cynick
New User

May 20, 2009, 4:39 PM


Views: 20463
Re: [vikas.deep] how do I use $1 in a $var for subsitutions

But I want to use variables, that come from <STDIN> or @ARGV..

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

my $d = "hello 123 world 456!";
my $b = <STDIN>;
my $c = <STDIN>;

$d =~ s/$b/$c/;

print "$d\n";

In Reply To



KevinR
Veteran


May 20, 2009, 4:42 PM


Views: 20461
Re: [cynick] how do I use $1 in a $var for subsitutions

Is this school work? If you want to use input from the user how come your script is not importing any input?
-------------------------------------------------


PearlFrog
New User

May 20, 2009, 5:06 PM


Views: 20459
Re: [cynick] how do I use $1 in a $var for subsitutions

I am having the same problem as cynick. I want backreferencing to work inside an interpolated string within a substitution replacement pattern. For example:

****Example 1 (backreferencing)****


Code
my $strExample = "This is a string."; 
$strExample =~ s/(.+)/start_\1_end/;
print $strExample;


will print


Code
"start_This is a string._end"


So far so good.

****Example 2 (interpolated strings as regex patterns)****


Code
my $strExample = "This is a string."; 
my $strFind = '(.+)';
my $strReplace = 'start__end';
$strExample =~ s/$strFind/$strReplace/;
print $strExample;


will print


Code
"start__end"


Still good.

****Example 3 (backreference inside interpolated strings as regex patterns)****


Code
my $strExample = "This is a string."; 
my $strFind = '(.+)';
my $strReplace = 'start_\1_end';
$strExample =~ s/$strFind/$strReplace/;
print $strExample;


will print


Code
"start__end"


instead of what I want it to print:


Code
"start_This is a string._end"


Help would be greatly appreciated.


vikas.deep
User

May 20, 2009, 8:11 PM


Views: 20445
Re: [PearlFrog] how do I use $1 in a $var for subsitutions

Dear friend
The computer never does what you want but always what you tell. See when you say

Code
$strExample =~ s/$strFind/$strReplace/;

Is it not another way of saying

Code
$strExample =~ s/This is a string./$strReplace/;

and where $strReplace is my $strReplace = 'start_\1_end';

Computer is simply doing it's job $1..$x are used to hold contents of group matches When there are no such group matches No doubt it will always print
"start__end"
Even as you yourself have written a good piece

Code
$strExample =~ s/(.+)/start_\1_end/;

I don't think I should post anything new over here

Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


KevinR
Veteran


May 20, 2009, 8:51 PM


Views: 20443
Re: [PearlFrog] how do I use $1 in a $var for subsitutions

PearlFrog,

Its considered rude to post a new question in another persons thread, even if they are somewhat similar. And anyway, you have the same question posted on at least one other perl forum where it has, I believe, been answered as best as possible:

http://www.tek-tips.com/viewthread.cfm?qid=1549585&page=1
-------------------------------------------------


PearlFrog
New User

May 22, 2009, 6:20 PM


Views: 20398
Re: [KevinR] how do I use $1 in a $var for subsitutions

KevinR,

My question is the exact same as cynick's question; I just gave more examples to clarify because another poster above did not seem to understand what cynick was asking, and no satisfactory answer had yet been given at the time. It would have been silly for me to start a new thread asking the same question. I was not trying to take over the thread from cynick; I was trying to get more discussion on the thread because at the time I posted there was still no solution given.


(This post was edited by PearlFrog on May 22, 2009, 6:21 PM)


KevinR
Veteran


May 22, 2009, 8:58 PM


Views: 20391
Re: [PearlFrog] how do I use $1 in a $var for subsitutions


In Reply To
KevinR,

My question is the exact same as cynick's question; I just gave more examples to clarify because another poster above did not seem to understand what cynick was asking, and no satisfactory answer had yet been given at the time. It would have been silly for me to start a new thread asking the same question. I was not trying to take over the thread from cynick; I was trying to get more discussion on the thread because at the time I posted there was still no solution given.


Fair enough Cool
-------------------------------------------------


vikas.deep
User

May 28, 2009, 5:08 AM


Views: 20290
Re: [PearlFrog] how do I use $1 in a $var for subsitutions


In Reply To
my $strExample = "This is a string.";
my $strFind = '(.+)';
my $strReplace = 'start_\1_end';
$strExample =~ s/$strFind/$strReplace/;
print $strExample;

will print
"start__end"

Dear Friend
I have checked twice it will never print

Code
"start__end"

It always prints

Code
start_\1_end[root@localhost Documents]#

Further I smell/suspect that there is more to it than meets my eyes.
In your own way are you saying that it can be done in perl 5.10.0

These days I am hearing about back referencing in perl 5.10.0.

Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


KevinR
Veteran


May 28, 2009, 10:54 AM


Views: 20285
Re: [vikas.deep] how do I use $1 in a $var for subsitutions

\1 appears to not be able to work in this context at all. And $strReplace needs to be eval'd anyway for it to work even if we use the correct variable $1:


Code
my $strExample = "This is a string.";  
my $strFind = '(.+)';
my $strReplace = '"start_$1_end"';
$strExample =~ s/$strFind/$strReplace/ee;
print $strExample;


Note the rather unintuitive use of double-quotes inside the single-quoted string $strReplace. And the use of "ee" on the end of the regexp to double eval the string to get it to work. Its messy.
-------------------------------------------------


alex_v2
User

May 28, 2009, 12:18 PM


Views: 20283
Re: [KevinR] how do I use $1 in a $var for subsitutions

Interesting I didn't even know you could double ee. See many years coding but a life time learning I guess.

-Alexander


KevinR
Veteran


May 28, 2009, 1:12 PM


Views: 20281
Re: [alex_v2] how do I use $1 in a $var for subsitutions


In Reply To
Interesting I didn't even know you could double ee. See many years coding but a life time learning I guess.

-Alexander


Personally I try and use "e" only to run code on the replacement side of a s/// regexp, and not for trying to interpolate strings that are otherwise not interpolated. It all seems very counter-intutitve to me and seems like a good way to introduce bugs into what might be otherwise good code. But then again, maybe it just reflects my own personal limitations and shouldn't be extrapolated into anything beyond that. Unsure
-------------------------------------------------