Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: little regex question

by Loops (Curate)
on Aug 01, 2013 at 09:33 UTC ( #1047404=note: print w/replies, xml ) Need Help??

in reply to little regex question

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.

Replies are listed 'Best First'.
Re: little regex question
by VGavara (Novice) on Aug 01, 2013 at 10:51 UTC

    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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1047404]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2017-12-16 03:52 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (448 votes). Check out past polls.