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:
Issues :matching variable $1,$2,... in replacement string

 



kennychen614
New User

Apr 19, 2011, 8:58 AM

Post #1 of 12 (10496 views)
Issues :matching variable $1,$2,... in replacement string Can't Post

I tried to use $1 in a variable, then use it in s///, but it fails to interpolated by perl. See my codes below


Code
my $v = q/There are 3 boys/; 
my $regx = q/\b(\d+)\b/;
my $str = q/$1 of 10/;

$v =~ s/$regx/$str/g;

print $1,"\n";
print "$v";


The output is as below,
3
There are $1 of 10 boys

If I put the content of $str directly to s/// as below,
$v =~ s/$regx/$1 of 10/g;
then it's ok.

Could someone please help?


Zhris
Enthusiast

Apr 19, 2011, 12:49 PM

Post #2 of 12 (10491 views)
Re: [kennychen614] Issues :matching variable $1,$2,... in replacement string [In reply to] Can't Post

Hi,


Code
my $str = q/$1 of 10/;

Using "q" will interprate the string literally as "$1 of 10", therefore in order for $1 to interpolate, you would need to use "qq".

However, since the regular expression hasn't been compiled yet, then $1 will interpolate as an empty string at this point, therefore it won't work as desired anyway.

Personally I would stick to using your working method, afterall theres not really any reason why you need to put the "search" string and "replace" string in their own variables.

Chris


(This post was edited by Zhris on Apr 19, 2011, 12:51 PM)


kennychen614
New User

Apr 20, 2011, 4:26 AM

Post #3 of 12 (10472 views)
Re: [Zhris] Issues :matching variable $1,$2,... in replacement string [In reply to] Can't Post

Thanks, Chris.

Previously, I wanted to make my program more flexible, i.e. allow user to input the search string and replacement string freely in command line. For example, "replace.pl <matching strings> <replacement strings>", but when I was trying to achieving this, I found that the matching variables $1,$2,... can not work as I expected.

So any alternative solution we can achieve this?


Zhris
Enthusiast

Apr 20, 2011, 6:55 AM

Post #4 of 12 (10465 views)
Re: [kennychen614] Issues :matching variable $1,$2,... in replacement string [In reply to] Can't Post

I'm not really sure how you would do it without using multiple expressions, or with user input, maybe there is a way using eval.

Here is my method which uses 2 expressions, but no user input:


Code
my $string = q/There are 3 boys/; 

my $search = q/\b(\d+)\b/;
$string =~ m/$search/;
my $replace = qq/$1 of 10/;
$string =~ s/$search/$replace/g;

print "$string";


I'm not sure if you will find this helpful. Others may have suggestions.

Chris


(This post was edited by Zhris on Apr 20, 2011, 7:16 AM)


miller
User

Apr 20, 2011, 9:03 AM

Post #5 of 12 (10454 views)
Re: [Zhris] Issues :matching variable $1,$2,... in replacement string [In reply to] Can't Post

There's no pretty way to do this type of thing, but this is one way:


Code
use strict; 
use warnings;

my $v = q{There are 3 boys};
my $regx = q{\b(\d+)\b};
my $str = q{$1 of 10};

$v =~ s/$regx/qq{"$str"}/eeg;

print "$v";



kennychen614
New User

Apr 21, 2011, 6:48 AM

Post #6 of 12 (10419 views)
Re: [miller] Issues :matching variable $1,$2,... in replacement string [In reply to] Can't Post

Thanks, Chris and Zhris!

If the user input parameter for replacement string is something like "$abc$1 of 10", i.e. user only want the $1 can be replaced with matching value, but keep $abc as raw string, then it will not be so easy.

Chris's solution has enlightened me to come up new thoughts to solve the problem. I can try to do a trick that replace $1 to an actual value in my program.


miller
User

Apr 21, 2011, 6:53 AM

Post #7 of 12 (10415 views)
Re: [kennychen614] Issues :matching variable $1,$2,... in replacement string [In reply to] Can't Post

Another way to do this:


Code
use strict;  
use warnings;

my $v = q{There are 3 boys};
my $regx = q{\b(\d+)\b};
my $str = q{$1 of 10};

my $sub = eval "sub { s/$regx/$str/g for (\@_) }";

$sub->($v);

print "$v";


- Miller


BillKSmith
Veteran

Apr 21, 2011, 2:15 PM

Post #8 of 12 (10388 views)
Re: [kennychen614] Issues :matching variable $1,$2,... in replacement string [In reply to] Can't Post

Another possible solution! Those e's are never easy to get right.


Code
my $v    = q/There are 3 boys/; 
my $regx = q/\b(\d+)\b/;
my $str = q/"$1 of 10"/;
$v =~ s/$regx/$str/ee;
print $1, "\n";
print "$v";

Good Luck,
Bill


Zhris
Enthusiast

Apr 21, 2011, 4:18 PM

Post #9 of 12 (10371 views)
Re: [BillKSmith] Issues :matching variable $1,$2,... in replacement string [In reply to] Can't Post

So hang on, whats this "double e" doing? Evaluate, evaluate. Would someone care to explain?

Does it firstly evaluate variables inside $str i.e. $1, then $str itself?

My initial attempt was something like /$search/eval{$replace}/eg; with no success.

Chris


(This post was edited by Zhris on Apr 21, 2011, 4:21 PM)


BillKSmith
Veteran

Apr 21, 2011, 9:04 PM

Post #10 of 12 (10347 views)
Re: [Zhris] Issues :matching variable $1,$2,... in replacement string [In reply to] Can't Post

I got the hint that /ee is required from the book "Perl Cookbook". (Chapter on expanding variables in user input) The quoting required some experimenting. I really do not fully understand the explanation (Summarized below)

The first \e works much like the eval {BLOCK} which you attempted. It is compiled along with the rest of the program. Subsequent \e's are like an eval "string".

Can anyone provide a better explanation?
Good Luck,
Bill


miller
User

Apr 21, 2011, 10:57 PM

Post #11 of 12 (10341 views)
Re: [miller] Issues :matching variable $1,$2,... in replacement string [In reply to] Can't Post


In Reply To

Code
use strict; 
use warnings;

my $v = q{There are 3 boys};
my $regx = q{\b(\d+)\b};
my $str = q{$1 of 10};

$v =~ s/$regx/qq{"$str"}/eeg;

print "$v";



Doing a double e modifier, basically just evals the right hand side twice. My original suggestion above becomes the following after the first eval


Code
$v =~ s/$regx/"$1 of 10"/eg;


Hence why I used multiple versions of the double quoted string.

I could also have done this for a bit more fun:


Code
$v =~ s/$regx/qq{qq{$str}}/eeg;



(This post was edited by miller on Apr 21, 2011, 10:58 PM)


Zhris
Enthusiast

Apr 22, 2011, 12:27 PM

Post #12 of 12 (10308 views)
Re: [BillKSmith] Issues :matching variable $1,$2,... in replacement string [In reply to] Can't Post

Thanks Bill and Miller for explaining further. I have a copy of the cookbook, but I never ever think to look in it for a solution to any of my problems, afterall thats what its there for.

Chris

 
 


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

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