Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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.


Comment on Re: little regex question
Select or Download Code
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
Node Status?
node history
Node Type: note [id://1047404]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-12-19 12:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (81 votes), past polls