Against all odds

According to several sites and Stack Overflow answers,[30] the CSS3 language is not regular, and cannot be parsed with a regular expression. So how was I able to do this? I think there are three contributing factors.

subset of strings. First, I was not dealing with the entire CSS3 language, but with only a distinct subset, selectors. That said, section 10.1 of [CSS3] asserts that even the selector syntax alone is an LL grammar, which implies it is not regular.

subset of task. I did not need to actually process the selector — I did not need to determine to which instance elements in the document the specified default rendition should apply — I merely needed to know whether a CSS3 processor could process the selector. Thus I had no need to parse selectors into their component segments, but rather had only the somewhat simpler task of ascertaining if a given string is a proper CSS3 selector or not.

subset of knowledge. Perhaps most importantly, I did not know it was impossible until after I’d done it. If I had read that CSS3 could not be matched by a regular expression before I had tried this, I may not have been smart enough to think well, I only need the selector bit; and I don’t need to parse it, only to validate, so maybe a regular expression will do. Quite likely I just would not have tried.

 

Some of the world’s greatest feats were accomplished by people not smart enough to know they were impossible.

 
 --Doug Larson