Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Just checked after reading your comment. Surprisingly to me, AFAs (Alternating Finite Automatons) do let you introduce the Complement operation into Regex while preserving the O(mn) complexity of running NFAs.

That's really subtle, because deciding Regex universality (i.e. whether a regex accepts every input) is PSPACE-COMPLETE. And since NFAs make it efficient to decide whether a regex matches NO inputs, any attempts to combine NFAs with regex Complement would trip on a massive landmine.





Actually, I've now checked more thoroughly, and RE+complement matching is O(n^2 m), which is not that good.

See https://en.wikipedia.org/wiki/Regular_language#Closure_prope...

The complement of a regular language is a regular language, and for any given regular language we can check whether a string is a member of that language in O(length of the string) time.

Yes, depending on how you represent your regular language, the complement operator might not work play nicely with that representation. But eg it's fairly trivial for finite state machines or when matching via Brzozowski derivatives. See https://en.wikipedia.org/wiki/Brzozowski_derivative

See also https://github.com/google/redgrep

Regular languages have a bunch of closure properties. In practice, intersection and complement are really useful. So you can say things like:

[regular_expression_for_matching_email_addresses] & ![my_list_of_naughty_words]

Exercise for the reader: expand the expression above to allow the town of 'Scunthorpe'.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: