Live data from Hacker News

Is there a regular expression to detect a valid regular expression?

stackoverflow.com

11–20 of 224 posts

Re: Is there a regular expression to detect a valid regular expression?

#11
post #3

Why would you want a regular expression to detect a valid regular expression?

A regular expression is a domain-specific language that defines a language consisting of all the strings it matches.

In principle, this serves as executable documentation.

A regular expression that matches valid regular expressions could be viewed as a human-readable documentation of valid regular expression syntax that has the valuable side-effect of being executable.

If, instead, we detect whether a string is a regular expression by compiling it, we may find it very difficult to read the compiler source code.

It is much more work to make an optimized regular expression engine that is also self-documenting than a regular expression.

Re: Is there a regular expression to detect a valid regular expression?

#12
post #10
post #5

Earlier quoted context omitted.

Probably to check if a regular expression is valid

Yes, checking to see if a regular expression is valid is useful but that wasn't what I was asking. Why specifically would you want to use a regular expression for this job?

Maybe because you can :/

Re: Is there a regular expression to detect a valid regular expression?

#16
post #4

IIRC, regex can be implemented by an NFA which can't solve balanced parentheses that are part of regex syntax.

Correct, though there are multiple interpretations of what a "regex" is. In the theoretical sense you're correct, though some (most programmers, I'd say) mean "PCRE" when they say regex. And PCREs are pretty powerful, see [1] for example

[1] https://news.ycombinator.com/item?id=9748736

Re: Is there a regular expression to detect a valid regular expression?

#17
post #2

I believe Zalgo has the answer to this, via an equivalent question. https://stackoverflow.com/questions/1732348/regex-match-open...

Perhaps the most epic reply I have ever seen on SO.

It’s a famous post on StackOverflow, but I don’t find it particularly helpful.

Re: Is there a regular expression to detect a valid regular expression?

#18
This immediately triggers the Liar's paradox [1].

Can a regular expression check its own validity?

How can you tell if it's lying?

Beware of bugs in the above code; I have only proved it correct, not tried it --Donald Knuth

[1] https://en.wikipedia.org/wiki/Liar_paradox

Re: Is there a regular expression to detect a valid regular expression?

#19
post #17

Earlier quoted context omitted.

Perhaps the most epic reply I have ever seen on SO.

It’s a famous post on StackOverflow, but I don’t find it particularly helpful.

I don't think that answer was written with the intent of being particularly helpful, I think it was written with a different goal in mind.

Re: Is there a regular expression to detect a valid regular expression?

#20
post #3

Why would you want a regular expression to detect a valid regular expression?

A regular expression is a domain-specific language that defines a language consisting of all the strings it matches. In principle, this serves as executable documentation. A regular expression that matches valid regular expressions could be viewed as a human-readable documentation of valid regular expression syntax that has the valuable side-effect of being executable. If, instead, we detect whether a string is a reg…

I think we have different definitions of human readable documentation. I'd find normal code, with or without appropriate comments, far more readable than a mess of regex symbols.
Post reply on HN