Hi,
Hopefully a simple question but my brain is hurting...
I want to make a regex substitution, using search and replace
patterns contained in variables. What I want to do is:
$f = "fred.abc";
$f =~ s/(.*)\.abc/$1.def/;
print "$f\n";
but where the two parts of the substitution are variables:
my $to_pattern = "(.*)\\.abc";
my $from_pattern = "\$1.def";
$f =~ s/$to_pattern/$from_pattern/;
print "$f\n";
Unfortunately this doesn't seem to work, where the first example
correctly prints out "fred.def" the second one doesn't seem to
do the back-substitution, and just prints "$1.def".
Is this possible, and if so, what quoting magic do I need to make
it work???
TIA,
Ian.
#!/usr/bin/perl -w
use strict;
my $f;
$f = "fred.abc";
$f =~ s/(.*)\.abc/$1.def/;
print "$f\n"; # prints "fred.def" (correct)
$f = "joe.abc";
my $to_pattern = "(.*)\\.abc";
my $from_pattern = "\$1.def";
$f =~ s/$to_pattern/$from_pattern/;
print "$f\n"; # prints "$1.def"
# end
--
Ian
"Tamahome!!!" - "Miaka!!!" 4 12059
On Thu, 02 Feb 2006 17:06:09 +0000, Ian wrote: but where the two parts of the substitution are variables:
my $to_pattern = "(.*)\\.abc"; my $from_pattern = "\$1.def"; $f =~ s/$to_pattern/$from_pattern/; print "$f\n";
Unfortunately this doesn't seem to work, where the first example correctly prints out "fred.def" the second one doesn't seem to do the back-substitution, and just prints "$1.def".
That is because the second part of the s/// construct is an ordinary
double quoted string. Once you have put a literal $ into a string, it is
not re-interpreted whenever the string is substituted. (What would you
expect
my $a = "X"; my $b = "\$a"; print "$b"
to print?)
Is this possible, and if so, what quoting magic do I need to make it work???
You need, in some sence, less quoting. One way is to re-write the
substitution as a little code block (in a string) and have it evaluated:
my $from_pattern = "\$1.'.def'";
$f =~ s/$to_pattern/eval($from_pattern)/e;
This being Perl, I await news of the 1867 other ways to do it... :-)
--
Ben.
On 2006-02-02, Ben Bacarisse <be********@bsb.me.uk> wrote:
[snip - understood] You need, in some sence, less quoting. One way is to re-write the substitution as a little code block (in a string) and have it evaluated:
my $from_pattern = "\$1.'.def'"; $f =~ s/$to_pattern/eval($from_pattern)/e;
Ta, this gives the effect I'm after. I was contemplating doing the whole
thing in an eval() but this is neater.
This being Perl, I await news of the 1867 other ways to do it... :-)
At least!
--
Ian
"Tamahome!!!" - "Miaka!!!"
In article <pa****************************@bsb.me.uk>, Ben Bacarisse
<be********@bsb.me.uk> wrote: On Thu, 02 Feb 2006 17:06:09 +0000, Ian wrote:
but where the two parts of the substitution are variables:
my $to_pattern = "(.*)\\.abc"; my $from_pattern = "\$1.def"; $f =~ s/$to_pattern/$from_pattern/; print "$f\n";
Unfortunately this doesn't seem to work, where the first example correctly prints out "fred.def" the second one doesn't seem to do the back-substitution, and just prints "$1.def".
.... You need, in some sence, less quoting. One way is to re-write the substitution as a little code block (in a string) and have it evaluated:
my $from_pattern = "\$1.'.def'"; $f =~ s/$to_pattern/eval($from_pattern)/e;
A second e modifier will force a second round of evaluation:
$f =~ s/$to_pattern/$from_pattern/ee; This being Perl, I await news of the 1867 other ways to do it... :-)
(1866 to go :)
FYI: this newsgroup is defunct. Try comp.lang.perl.misc in the future.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
---------------------------------------------------------- http://www.usenet.com
On Thu, 02 Feb 2006 12:26:21 -0800, Jim Gibson wrote: In article <pa****************************@bsb.me.uk>, Ben Bacarisse <be********@bsb.me.uk> wrote:
On Thu, 02 Feb 2006 17:06:09 +0000, Ian wrote:
> but where the two parts of the substitution are variables: > > my $to_pattern = "(.*)\\.abc"; > my $from_pattern = "\$1.def"; > $f =~ s/$to_pattern/$from_pattern/; > print "$f\n"; > > Unfortunately this doesn't seem to work, where the first example > correctly prints out "fred.def" the second one doesn't seem to do the > back-substitution, and just prints "$1.def". ... You need, in some sence, less quoting. One way is to re-write the substitution as a little code block (in a string) and have it evaluated:
my $from_pattern = "\$1.'.def'"; $f =~ s/$to_pattern/eval($from_pattern)/e; A second e modifier will force a second round of evaluation:
$f =~ s/$to_pattern/$from_pattern/ee;
Lovely! I did not know that.
This being Perl, I await news of the 1867 other ways to do it... :-)
(1866 to go :)
No, that's the one as far as I'm concerned!
FYI: this newsgroup is defunct. Try comp.lang.perl.misc in the future.
Thanks for the heads up.
--
Ben. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Reinhold Birkenfeld |
last post by:
Hello,
I recently ported a simple utility script to analyze a data file from
Perl to Python that uses regex substitutions, not more complex than
re1 = re.compile(r"\s*<.*>\s*")
re2 =...
|
by: Justin |
last post by:
Hi,
First off, I must apologise for cross posting.
I am having difficulty creating a pdf document using perl cgi to do
substitution for multiline pdf form fields. I created a pdf template/file...
|
by: Just Me |
last post by:
I want to use regular expressions to search a string, give the user the
option of replacing, and then maybe replacing the data - using reg
expressions for the search and the replace strings.
...
|
by: |
last post by:
I tried
Dim s As String = "1 2 3 4 5"
Regex.Replace(s, "3", "6")
but no luck, nothing was changed?
|
by: Chris |
last post by:
How Do I use the following auto-generated code from The Regulator?
'------------------------------------------------------------------------------
' <autogenerated>
' This code was generated...
|
by: MtiPaulo |
last post by:
I know FoxPro has excellent macro substitution but I am trying to find
through MS Access. Wondering if MS Access has Macro Substitution? I will
give you my basic example.
In FoxPro Language...
|
by: Steve |
last post by:
Hi All,
I'm having a tough time converting the following regex.compile patterns
into the new re.compile format. There is also a differences in the
regsub.sub() vs. re.sub()
Could anyone lend...
|
by: morleyc |
last post by:
Hi, i would like to remove a number of characters from my string (\t
\r \n which are throughout the string), i know regex can do this but i
have no idea how. Any pointers much appreciated.
Chris
|
by: Chris O |
last post by:
"Larry Lowe" <llowejr@usa.netwrote in message
news:ad17577.0308140714.1b14d778@posting.google.com...
Hi Larry. Some responses aren't very helpful. I'll
try to do a little better.
The first...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |