Skip to content

regex matches everything for empty string ("") as a pattern #896

Answered by BurntSushi
mrl5 asked this question in Q&A
Discussion options

You must be logged in to vote

Yeah absolutely this is correct. What do you think the behavior should be?

The behavior is solidly correct on both pragmatic and theoretical fronts:

  • I don't know of any regex engine that behaves differently. If you do, I would be happy to hear about it.
  • From a theoretical perspective, the empty regex describes the empty language. The empty language is a sub-language of every other language. It's like how the empty set is a subset of every set.

It is possible to write a regex that is the opposite of the empty set, i.e., one that will not match anything. You could write it like so: [a&&b]. Currently though, such patterns won't compile. (I intend to lift that restriction some day.)

Replies: 3 comments 7 replies

Comment options

You must be logged in to vote
2 replies
@leighmcculloch
Comment options

@mrl5
Comment options

Comment options

You must be logged in to vote
2 replies
@mrl5
Comment options

@BurntSushi
Comment options

Answer selected by mrl5
Comment options

You must be logged in to vote
3 replies
@BurntSushi
Comment options

@zhaojiangbin
Comment options

@BurntSushi
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants