On Fri, 12 Jan 2007, Sam Martin wrote:

> On 1/12/07, Dan Drake wrote:
>> I'm looking for a regular expression that's guaranteed to never match
>> anything.
>
> How about "(?!)" ?
>
> For the record, I googled '"regular expression" "never matches"', and 
> came up with the following from a paper about creating a regex debugger 
> in perl (http://perl.plover.com/Rx/paper/):
>
> ". . . (?!) is a pattern that never matches anything, so when the regex 
> engine reaches it, it is forced to backtrack. "


In case anyone is interested in more on this, from "man perlre":

      `(?!pattern)'
                A zero-width negative look-ahead assertion.  For
                example `/foo(?!bar)/' matches any occurrence of
                "foo" that isn't followed by "bar".  Note however
                that look-ahead and look-behind are NOT the same
                thing.  You cannot use this for look-behind.

                If you are looking for a "bar" that isn't preceded
                by a "foo", `/(?!foo)bar/' will not do what you
                want.  That's because the `(?!foo)' is just saying
                that the next thing cannot be "foo"--and it's not,
                it's a "bar", so "foobar" will match.  You would
                have to do something like `/(?!foo)...bar/' for
                that.   We say "like" because there's the case of
                your "bar" not having three characters before it.
                You could cover that this way:
                `/(?:(?!foo)...|^.{0,2})bar/'.  Sometimes it's
                still easier just to say:

                    if (/bar/ && $` !~ /foo$/)

                For look-behind see below.