Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Compiling regular expressions to perform substitution

by Stegalex (Chaplain)
on Apr 11, 2002 at 13:04 UTC ( [id://158301]=perlquestion: print w/replies, xml ) Need Help??

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

I am writing a program which will read a bunch of substitution regular expressions from a configuration file and then apply them to the contents of other files. The problem I am running into is that the regexps don't seem to be compiling because they just aren't working. The following code illustrates what I mean:
#!/usr/bin/perl -w $regex = "s/foo/bar/g"; $compiled = qr/$regex/; # program works the same # whether I do this or not $string = "foo fru foodyfoo"; $string =~ $compiled; print "$string\n"; # no substitution! $string is still # "foo fru foodyfoo" instead of # "bar fru bardybar".
I know this is probably a stupid question but that's only because I am a stupid person. Sorry if my question makes you groan with disgust. I like chicken.

Replies are listed 'Best First'.
Re: Compiling regular expressions to perform substitution
by broquaint (Abbot) on Apr 11, 2002 at 13:11 UTC
    The reason they aren't compiling properly is that s/// is an operator. If you are going to be compiling regexps from a string, you need only compile the left-hand side of a s///, as the right-hand side is essentially a double quoted string.
    use strict; my $re = qr/foo./; my $str = "foo bar baz"; $str =~ s/$re/FOOD /; print "$str\n"; __output__ FOOD bar baz

    HTH

    broquaint

      Well pluck my feathers and fry me in a vat! You're right. Thanks! I like chicken.
It doesn't mean what you think it means
by Fletch (Bishop) on Apr 11, 2002 at 13:14 UTC

    qr// only compiles regular expressions, not substitution operators. What you've done is compiled a regular expression for matching `s/foo/bar/g'. You can use compiled regexen on the left hand side of an s/// expression, but you can't use one to capture the whole expression.

    Now if what you're trying to do is capture an operation or three to be performed on a string that you can select programattically, you possibly could use a hash of coderef like this:

    my %mungers = ( foo => sub { $_[0] =~ s/foo/bar/g }, fru => sub { $_[0] =~ s/(fru.*)/uc($1)/e }, ); if( $phase_of_moon > WAXING_GIBBOUS ) { $mungers->{foo}->( $string ); } else { $mungers->{fru}->( $string ); }

    Update: Feh, as was pointed out that first -> inside the if/else blocks is extraneous. That's what I get for posting while trying to eat breakfast.

      Slight modification needed to make this compile. Here it is:
      my %mungers = ( foo => sub { $_[0] =~ s/foo/bar/g }, fru => sub { $_[0] =~ s/(fru.*)/uc($1)/e }, ); if( $phase_of_moon > WAXING_GIBBOUS ) { $mungers{foo}->( $string ); # this line changed } else { $mungers{fru}->( $string ); # this line changed }
      Again, thanks for everyone's help. I like chicken.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-26 00:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found