Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Perl regular expression question

by Anonymous Monk
on Aug 25, 2005 at 08:03 UTC ( #486468=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a small code sniplet here which i could not understand.. im jst a perl newbie..could you tell me under which condition will the if statement be true..
if(m|$input|o) { print"hello"; }
I could not understand what the "m|$input|o" it looks like a regular expression.. could not figure out what it means..

Comment on Perl regular expression question
Download Code
Re: Perl regular expression question
by davido (Archbishop) on Aug 25, 2005 at 08:12 UTC

    It will be true if $input represents a pattern that will be a positive match against the string held in $_

    The /o modifier has fallen out of favor nowadays, largely replaced by the qr// operator. But in this case what it means is that once the RE is compiled once, it won't be recompiled again, even if the value of $input changes. The choice of regexp delimiters as the '|' character seems a little ackward, as it makes alternation difficult.

    perlre, perlrequick and perlretut can be helpful.


    Dave

Re: Perl regular expression question
by gargle (Hermit) on Aug 25, 2005 at 08:12 UTC
    if(m/foo/)... tests whether the current string contains the string foo. The 'm' at the beginning gives you a choice of delimiters (you're not limited to the slashes!) The 'o' at the end makes perl compile the regexp only once.
Re: Perl regular expression question
by inman (Curate) on Aug 25, 2005 at 08:22 UTC
    Take a look at perlrequick. It gives you a quick overview of Regular Expressions in Perl. My explanation of your code is as follows:

    1. The m tells perl to start a match.
    2. The | character is used as a delimiter. Using / is normal but any delimiter can be used. If you use / then you don't need to start with an m
    3. The $input is interpolated (The content of the variable is placed in the regular expression)
    4. The o modifier signifies that the regular expression is compiled once. Changing the value in $input will not change your regex.
    5. The whole regular expression is matched against the value held in $_ by default. You would use the =~ operator to match a particular variable.
      Thank you all for providing such good explanation and providing links for further study .. Thank you very much!!!
Re: Perl regular expression question
by dbae (Beadle) on Aug 25, 2005 at 08:22 UTC
    For further information, give the command
    perldoc perlre
    perlre is the manual page for regular expressions in perl.
    perldoc accesses this manual page.

    perlre probably contains much more than you want at this stage, but at least you would know the kind of thing that's available for when you need it in the future.

Re: Perl regular expression question
by tlm (Prior) on Aug 25, 2005 at 10:33 UTC

    In addition to the very helpful comments you've already received, I'd like to point out that, as a rule, I'd "metaquote" the scalar variable in the regex, just in case it contains characters (e.g. +) that otherwise would have special meaning in a regular expression:

    m|\Q$input|o

    See the aforementioned perlre, and while you're at it also check out quotemeta.

    the lowliest monk

      Unless of course $input were a stored regular expression (such as qr//), where the matching behavior was expressly desired.

      --
      [ e d @ h a l l e y . c c ]

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (12)
As of 2014-11-26 20:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (172 votes), past polls