Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

simple regular expression

by iaw4 (Monk)
on Aug 15, 2008 at 14:55 UTC ( [id://704557]=perlquestion: print w/replies, xml ) Need Help??

iaw4 has asked for the wisdom of the Perl Monks concerning the following question:

dear perlmonks---again, easier to show than to explain:
/usr/bin/perl -w use strict; my $text= "start \\chapter{hello}\n end\n"; my $inpattern = qr/\\chapter\{(.*)\}/; my $outpattern = " -- FOUND \$1 -- "; $text =~ s/$inpattern/$outpattern/ge; print "$text\n";
How do I get the output I want, which is "FOUND hello", not "FOUND $1". Advice appreciated. regards, /iaw

Replies are listed 'Best First'.
Re: simple regular expression
by Fletch (Bishop) on Aug 15, 2008 at 14:59 UTC

    Needs more cowbell /es. And slightly different quoting.

    #/usr/bin/perl -w use strict; my $text= "start \\chapter{hello}\n end\n"; my $inpattern = qr/\\chapter\{(.*)\}/; my $outpattern = q{" -- FOUND $1 -- "}; $text =~ s/$inpattern/$outpattern/gee; print "$text\n";

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: simple regular expression
by graff (Chancellor) on Aug 15, 2008 at 23:55 UTC
    Does the following come close to what you are looking for? I've modified your snippet so that a pattern is actually read from a file. Putting the s/// operation inside a string eval is an effective way to do what you want (my version does have "FOUND hello" in its output ). Note the backslash before "$text", so that this variable does not get interpolated into the string before it is evaluated as code:
    #!/usr/bin/perl use strict; use warnings; my $text = "start \\chapter{hello}\n end\n"; my $patternreadfromfile = <DATA>; chomp $patternreadfromfile; my $inpattern = qr/\\chapter\{(.*)\}/; eval "\$text =~ s/$inpattern/$patternreadfromfile/g"; print "$text\n"; __DATA__ -- FOUND $1 --

    (update: sorry, this node should have been a reply to your other node in this thread, at Re^2: simple regular expression)

Re: simple regular expression
by AnomalousMonk (Archbishop) on Aug 15, 2008 at 16:32 UTC
    Fletch is quite right of course (I think; haven't tested it personally), but as a matter of curiosity, why do you not take the simpler approach of:

    $text =~ s/$inpattern/ -- FOUND $1 -- /g;

    No doubt this is just a part of a much larger script or perhaps an exercise, but, as I say, I was just curious.

      indeed, it is part of a larger exercise. and thank you.
      In my quest to simplify, I simplified too much. Let me try again:
      #!/usr/bin/perl -w use strict; my $text= "start \\chapter{hello}\n end\n"; my $patternreadfromfile= "-- FOUND \$1 -- "; my $inpattern = qr/\\chapter\{(.*)\}/; my $outpattern = q{$patternreadfromfile}; $text =~ s/$inpattern/$outpattern/gee; print "$text\n"; now q{} escapes too much...
        With nested regex evaluations like m//ee s///ee, you have to be very clear about what expression you are operating on at each stage of /e-valuation. It's best to work backwards.

        In your latest example, and working backwards from the regex replacement field, the expression $outpattern, which was defined with single- (i.e., non-interpolating) quotes, evaluates to the string '$patternreadfromfile', literally. (The single-quotes are not part of the string.) Evaluating again (the second e in the /gee), the expression $patternreadfromfile evaluates to the string '-- FOUND $1 --'. This string is what is substituted into the original string.

        It's as if you just need one more level of double-quotish interpolation to get what you want!

        If the first evaluation yielded the string 'qq{-- FOUND $1 -- }', you could evaluate that a second time as an expression to get what you want. So $outpattern could be defined as qq{ qq{$patternreadfromfile} } and you're home and dry!

        I hope this rather involved and, I hope, not too incoherent explanation will be helpful.

        (BTW -- I assume the multiple levels of string definition and interpolation are needed in the final application. In your second code example as it stands, they are not; $patternreadfromfile could be interpolated more directly.)

        use warnings; use strict; my $text= "start \\chapter{hello}\n end\n"; my $patternreadfromfile= "-- FOUND \$1 -- "; my $inpattern = qr/\\chapter\{(.*)\}/; # my $outpattern = q{$patternreadfromfile}; # # $outpattern eq '$patternreadfromfile' # # outputs: 'start -- FOUND $1 --\n end' my $outpattern = qq{ qq{$patternreadfromfile} }; # $outpattern eq 'qq{-- FOUND $1 -- }' # eval($outpattern) eq '-- FOUND capture_group_1_contents -- ' # outputs: what you want (i think) $text =~ s/$inpattern/$outpattern/gee; print "$text\n";

        Update:

        1. ... $patternreadfromfile could be interpolated more directly.
          What I had in mind was that the pattern could be interpolated in just one evaluation step. On further consideration, I don't think this can be done. (But see graff's note below on an approach not involving s///e substitution replacement evaluation.)
        2. 2017 Feb 18: Just noticed that I was trying to apply an /e modifier to a m// match in the first paragraph above. Don't know where that came from. Fixed.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://704557]
Approved by grep
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-06-18 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.