Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: little regex question

by Loops (Curate)
on Aug 01, 2013 at 09:33 UTC ( [id://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:

    [^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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1047404]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-23 11:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found