in reply to Regular expressions Containing Octal values?


Use ${1} instead of \1:     s/(\d+ *)L\-homoserine/${1}2\-Amino\-4\-hydroxybutyric\ acid/;

It is best not to use \1 on the right hand side of a s///. See also "Warning on \1 vs $1" in perlre.

You can omit the escapes to make it a little cleaner:     s/(\d+ *)L-homoserine/${1}2-Amino-4-hydroxybutyric acid/;

Also, the match doesn't work for cases with no leading number (you seem to suggest that this is possible). Therefore, something like this might be better:     s/(\d*\s*)L-homoserine/${1}2-Amino-4-hydroxybutyric acid/;

--
John.

Replies are listed 'Best First'.
Re: Re: Regular expressions Containing Octal values?
by arunhorne (Pilgrim) on May 17, 2002 at 10:11 UTC
    Thanks John. However when I include ${1} in my regular expressions I now get a lot of "Use of uninitialised value" errors. Any ideas?
      Sorry, that was my fault. I was generating text strings as regular expressions (I have a separate script that executes a file containing a set of regexes against another file) and forgot to escape the \${1}. Sorry.
•Re: Re: Regular expressions Containing Octal values?
by merlyn (Sage) on May 17, 2002 at 15:24 UTC
    No. $1 is the value of the first subexpression from the previous successful match, which will be interpolated during the compilation of this regular expression. Definitely not what you want.

    I give some solutions in my other node in this thread.

    -- Randal L. Schwartz, Perl hacker


    update: And of course, that only makes sense if we're talking about the LHS, but it's the RHS in this example. Sorry.

      I don't get it. Isn't the previous successful match at the LHS of the s///? Isn't that interpolated into this regex? Like this:
      #!/usr/bin/perl -wl use strict; $_ = "3 L-homoserine"; /(\w-\w)/; print $1; s/(\d*\s*)L-homoserine/${1}2-Amino-4-hydroxybutyric acid/; print; $_ = "L-homoserine"; s/(\d*\s*)L-homoserine/${1}2-Amino-4-hydroxybutyric acid/; print; __END__ Prints: L-h 3 2-Amino-4-hydroxybutyric acid 2-Amino-4-hydroxybutyric acid

      --
      John.

        Ahh, I totally missed that we were talking about the RHS (a qq string) instead of the LHS (a qr string). In that case, $1 (and in this case, ${1}) are the only things that should be used. The \1 construct works only as an error-correction DWIM that as shown can be sometimes ambiguous.

        -- Randal L. Schwartz, Perl hacker