Friday, April 20, 2012

Java RegEx that matches anything BUT literal string 'NIL' or 'nil'

OK, guys. Here's a Java interview-type question that seems to have stumped some very smart people around here. They actually need this for production code, so it's more than just an interview puzzler.



They need a regular expression, in Java, that returns true if a string literal is anything other than the 3-letter word NIL. The test needs to be case insensitive, and the RegEx itself must do all the work.



So, the RegEx should reject NIL, nil, NiL, nIL, and so on.



It should, however, accept: nile, anil, will, zappa-nil-a, and the empty string.



How many Java developers does it take to write a trivial RegEx? Apparently a lot!





No comments:

Post a Comment