Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

little regex question

by anaconda_wly (Scribe)
on Aug 01, 2013 at 09:29 UTC ( #1047402=perlquestion: print w/ replies, xml ) Need Help??
anaconda_wly has asked for the wisdom of the Perl Monks concerning the following question:

I saw in perl tutorial:  /[^a]at/;  # doesn't match 'aat' or 'at' I understand the first doesn't match while not understand the second one. Why not 'at'? Does /aat/ match at? or a can be viewed as blank?

Comment on little regex question
Download Code
Re: little regex question
by Loops (Hermit) on Aug 01, 2013 at 09:33 UTC
    Hi there,

    The answer is that the [^a] phrase means any character other than 'a', but it still must match one character. Since the string 'at' only has 2 characters, there is no character at all to match.

    So what is actually called for is a negative-lookbehind: /(?<!a)at/ that ensures 'at' isn't preceded by an 'a' character, it will still match 'at' when it begins in the first character position of a string.

    /[^a]?at/ Would behave as you expected the original regex to behave. Update: ugh, this was a little optimistic, it will do the wrong thing with 'aat' now.

      Just note the difference between these two regular expressions:

      [^a]at

      Your regex, will match any expression with three characters where the first one is not 'a' and the rest is 'at'

      [^a]?at

      The regex you would expect, will match any expression ending with 'at' and starting AT MOST (repetition operator ?) by any character not being 'a'

      Not the suitable place to post it since it seems I'm answering to Loops, sorry. The response was directed to anaconda

Re: little regex question
by vinoth.ree (Parson) on Aug 01, 2013 at 09:53 UTC
    Hi anaconda_wly

    Why not 'at'?

    The Perl Tutorial answers your questions as Both [...] and  [^...] must match a character

    The special character ^ in the first position of a character class denotes a negated character class, which matches any character but those in the brackets. Both ... and ^... must match a character, or the match fails.


    All is well
      Thank loop and vinoth both. Yes I noticed ... must match a character in the tutorial but didn't understand ^... must match a character. Now I see. Thanks!

        What tutorial (link)?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2014-09-15 05:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (145 votes), past polls