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

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?

Replies are listed 'Best First'.
Re: little regex question
by Loops (Curate) 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:


      Your regex, will match any expression with three characters where the first one is not 'a' and the rest is '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 (Monsignor) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1047402]
Approved by Loops
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2018-05-25 11:26 GMT
Find Nodes?
    Voting Booth?