Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Problem using value of variable in a regular expression

by Anonymous Monk
on Jul 30, 2010 at 10:17 UTC ( [id://852056]=perlquestion: print w/replies, xml ) Need Help??

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

In my perl program I am trying to find out if there is a pattern *E in the file. So, I am using the following code.
$failmsg = "\*E"; if (/$passmsg/) { $result = 1; } if (/$failmsg/) { $result = 0; }
I am getting error, Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HERE E/ at But, when I do the following, I don't get any error.
if (/$passmsg/) { $result = 1; } if (/\*E/) { $result = 0; }
Any help is greatly appreciated.

Replies are listed 'Best First'.
Re: Problem using value of variable in a regular expression
by moritz (Cardinal) on Jul 30, 2010 at 10:25 UTC
    A string used in a regex is used as a regular expression.

    I guess you've understood this, but your attempt to escape the star didn't work:

    $ perl -wE 'say "\*E"' *E

    The double-quoted string ate the backslash, the regex just sees *E.

    There are several solutions:

    # ugly, but works: my $failmsg = "\\*E"; if (/$failmsg/) { ... } # nicer, since you don't have to do the escaping yourself $failmsg = '*E'; if (/\Q$failmsg\E/) { ... } # if you want to quote regular expressions, do this: $failmsg = qr{\*E}; if ($_ =~ $failmsg) { ... }

    This last solution is nice because it accepts modifiers, so for example if you want to do a case-insensitive match, you can write $failmsg = qr{\*E}i;.

    Perl 6 - links to (nearly) everything that is Perl 6.
      And some other ways:
      my $failmsg = '\*E'; # Single quotes prevent interpolation my $failmsg = '[*]E'; # Character class makes it not special. my $failmsg = "*E"; if (index($_, $failmsg) >= 0) # Don't use a regexp.
      moritz, Thanks for the quick reply. But, what surprises me is double-quotes eating up the backslash. This is very arbitrary, unintuitive(at least to me), and also undesirable. Do you know if this is documented anywhere? Thanks for all the help.

        It's not undesirable at all. In double quotes, the backslash is indicates the start of an escape allowing "\n" to mean newline, "\"" to mean a double quote, etc. Double quotes are documented in perlop.

        You're just using the wrong tool.

        $failmsg = qr/\*E/;
        Documented in various places. perlop, sections Quote-Like Operators and Gory details of parsing quoted constructs, and perldata, section Scalar value constructors.
Re: Problem using value of variable in a regular expression
by FunkyMonk (Chancellor) on Jul 30, 2010 at 10:29 UTC
    change your double quotes to single:
    $failmsg = '\*E';

    Or, double up the backslashes

    $failmsg = "\\*E";
Re: Problem using value of variable in a regular expression
by JediWizard (Deacon) on Jul 30, 2010 at 14:30 UTC

    You might also consider using quotemeta or the \Q and \E operators.

    My other thought here, because you don't appear to need the power of a regular expression (you are just looking to see if one string contains another), index will give you that information with much less overhead.


    They say that time changes things, but you actually have to change them yourself.

    —Andy Warhol

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-23 09:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found