Regular expression guaranteed to fail

Eric Brunel eric_brunel at despammed.com
Fri Aug 20 08:06:17 EDT 2004


Des Small wrote:
> I want to use sets and regular expressions to implement some
> linguistic ideas.  Representing sounds by symbols, and properties
> (coronal or velar articulation; voicedness) by sets of symbols with
> those properties, it is natural to then map these sets, and
> intersections of them, to regular expressions to apply to strings.
> 
> The question is, what regular expression should correspond to the
> empty set?  I've provisionally gone with "(?!.*)", i.e., the negation
> of a look-ahead which matches anything.  Is there an established idiom
> for this, and is that it?  And if there isn't, does this seem
> reasonable?

I also looked for a never-matching re just a few days ago and ended up with 
"^(?!$)$". It's certainly not more "standard" than yours, but I find it a wee 
tad more readable (for a regular expression, I mean...): it's quite clear that 
it requests a string start not followed by a string end and followed by a string 
end, which is guaranteed to never happen. Yours is a bit harder to explain. Mine 
may also be more efficient for very long strings, but I can be wrong here.

See what other people think...
-- 
- Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com




More information about the Python-list mailing list