My favorite example that I see pretty often is "In posix grep, what's a regex that matches lines that don't contain the word `test`?"
The X answer is a lecture on language theory including the construction and inversion of finite state automata. Absolutely interesting, but a long and dense chapter in a CS textbook with no practical applications.
> The X answer is a lecture on language theory including the construction and inversion of finite state automata.
To be helpful and educational, one must describe/illustrate the failure mode of X. For example just demonstrate how unwieldy pure regex is for more constrained question like “regex to check if there is no ‘ab’ within 3 char string”. “a[^ab].|.a[^b]|[^a][^a].”. (Solution complexity grows with length of the string within which we are searching)
> (Solution complexity grows with length of the string within which we are searching)
No it doesn't. It's something like '([^a]|a[^b]|a$)*'.
In general it scales like '([^a]|a([^b]|$)|ab([^c]|$)|abc([^d]|$)|...)*' (ie, quadratically with the length of the needle, if I haven't overlooked a optimization), but the complement of a regular language is regular, and fixed strings are always regular, so it's always a pure regular expression (including being independent of haystack length).
And, proving that any curt correction of a silly error will likely contain its own silly error, I think that should actually be '^([^a]|a[^b]|a$)*$', since grep defaults to searching rather than matching.
The X asked for a regex, not a command-line parameter. It's possible that the user was actually using POSIX grep, in which case Y ("don't invert the regex, use this command-line parameter to invert what's done with the result of the regex") is the answer they actually need. It's also possible that they're using some other tool, which they know implements POSIX-compatible regexes, but doesn't have an easy way to invert the usage; in which case the user really does need an answer to X (i.e., a way to invert the regex).
And how do I enable Perl regex for e.g., the Snowflake SQL regex functions? ;)
I’m laughing because I had this exact XY problem. Searched StackExchange for a potential workaround for a certain regex I wanted to match, and the answer said to use Perl regexes instead. Cool... except as far as I can’t tell the database we use doesn’t support that. I rolled my eyes and went to look at other search results.