LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-31-2014, 06:09 AM   #1
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Rep: Reputation: Disabled
Perl : Regex inside a variable


Hi,

I am doing a search a replace using regex in PERL. I am reading the search and replace string from a file storing in a variable and trying to replace it.

$srcstr='fopen\((\w+)\,(\w+)\)'; #read from a file
$deststr='fopen\($2\,$1)'; #read from a file

$line =~ s/$srcstr/$deststr/;

This search and replace doesnt work , when we use capturing and backreference as a variable in Perl.
 
Old 10-31-2014, 06:48 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You might wish to clarify a little more on "doesn't work"? Are you getting no output, wrong output, error messages, etc...

Also, please enclose data or code in [code][/code] tags
 
Old 10-31-2014, 07:02 AM   #3
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
Quote:
print "Before: ", $line;
$srcstr='fopen\((\w+)\,(\w+)\)';
$deststr='fopen\($2\,$1)';
$line =~ s/$srcstr/$deststr/;
print "Src: $srcstr Dest: $deststr \n";
print "After: ", $line, "\n";
Expected Output
Before: fopen(fname,filehandle);
Src: fopen\((\w+)\,(\w+)\) Dest: fopen\($2\,$1)
After: fopen(filehandle,fname);

Actual Output
Before: fopen(fname,filehandle);
Src: fopen\((\w+)\,(\w+)\) Dest: fopen\($2\,$1)
After: fopen\($2\,$1);



I wanted it to work as expected.

Last edited by jags1984; 10-31-2014 at 07:05 AM.
 
Old 10-31-2014, 07:47 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You may have to wait for one of the perl experts, but I think your issue is one of timing.

At the point where you set deststr your positional items have no values and hence the string is set literally.
However your srcstr is interpreted once inside the search, so it is grabbing all that matched and then changing it for the literal string.

There may be some perl magic one of the gurus knows or a simple step you need to include, so hopefully they will jump on and advise

I would check some of your escapes in the deststr as well as there are either some missing or too many in the first place.
 
Old 10-31-2014, 11:45 PM   #5
kevnuke
LQ Newbie
 
Registered: Oct 2014
Location: Bakersfield, CA
Distribution: None, currently.
Posts: 2

Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
..but I think your issue is one of timing.
Does Perl have coroutine functionality?
 
Old 11-01-2014, 06:03 AM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Perl has nearly everything.

The problem is one of substitution. What you are trying to do is a double interpretation of the "$deststr". First to replace the variable with the string... then again to replace the back references.

The "s" operator does only single substitution, like all handling of " strings. The first parameter is then interpreted (again), but the second is not - it is presumed to be a constant.


One way to get this is:
Code:
print "Before: ", $line,"\n";
$srcstr='fopen\((\w+)\,(\w+)\)';
$deststr='fopen($2,$1)';
$s="\$line =~ s/$srcstr/$deststr/;";
eval $s;
#$line =~ s/$srcstr/$deststr/;
print "Src: $srcstr Dest: $deststr \n";
print "After: ", $line, "\n";
This is not necessarily the best way as it requires the $line, $srcstr and $deststr to be global.

Another way (which is more likely to be what you want) uses the "e" modifier (twice). This also requires a slight change in the specification of the value of $deststr:

Code:
print "Before: ", $line,"\n";
$srcstr='fopen\((\w+)\,(\w+)\)';
$deststr='"fopen($2,$1)"';
$line =~ s/$srcstr/$deststr/ee;
print "Src: $srcstr Dest: $deststr \n";
print "After: ", $line, "\n";
Note the value of $deststr - it now has embedded the evaluation with a quoted string( a '"..."'). This is necessary because the e modifier causes the value to evaluated - causing string substitution which handles the $1 and $2 substitutions, then evaluates that (which removes the "..."), which is then used for the replacement string.

If the " quoting in $deststr is missing, then it will evaluate the string as the perl expression fopen(filehandle,fname), which would return nothing (usually, as the fname just might not exist). With the quotes the evaluation returns the string you want.

Last edited by jpollard; 11-01-2014 at 06:09 AM.
 
2 members found this post helpful.
Old 11-01-2014, 06:26 AM   #7
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Edit: never mind - jpollard had better suggestion

Last edited by smallpond; 11-01-2014 at 06:29 AM.
 
Old 11-02-2014, 11:57 PM   #8
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
Thankyou jpollard,


It works
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Confusing issue with Perl regEx - Regex check seems to require variable being set EnderX Programming 1 09-07-2013 04:36 AM
[SOLVED] setting new variable to regex of another without changing the other variable in perl Eppo Programming 1 12-22-2011 02:35 PM
using variable inside regex pauld Programming 10 09-15-2011 09:33 AM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
[SOLVED] How to include a string variable in a regex (Perl) MTK358 Programming 5 01-09-2010 11:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration