Live data from Hacker News

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

stackoverflow.com

31–40 of 224 posts

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

#31
post #20

Earlier quoted context omitted.

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.

Regular expressions are not my favourite either, but one thing I will say is that normal code becomes difficult to read over time as features are bolted on and optimizations are added to address performance concerns.

Whereas, some form of executable DSL can serve as self-testing documentation indefinitely. DSLs can provide a separation of concerns, where the syntax is designed for readability, and the engine is designed for performance.

Modern Regexen are a dumpster fire, of course, but you asked why someone might want to, not whether this is something Everyone might want to do.

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

#33
post #27
post #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

That does not really apply. A compiler can compile itself, it can check if its own code is valid.

You are necessarily claiming that a compiler is incapable of false negatives.

e.g incorrectly validating its own code.

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

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

#34
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.

In SO's hall of fame of regular members being feed up of a common yet stupid question always coming back up it's up there alongside the thread about making addition with jQuery

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

#35

"Evaluate it in a try..catch or whatever your language provides." "That's not very enterprisey of you" Oooh, I'm laughing so hard it hurts. It's been a particularly 'enterprisey' week at work.

"We could do it in a easy two liners that make sense, but, and hear me out here, what if we instead built an entire 10k LOC system that would barely work half of the time, using dozens of man hours for it ? And then never re-use it nor train anyone about it so that it becomes a mythical black box after your contract expires ?"

"Well, I gave you my recommendation but hey at the end of the day you're the customer and I'm paid by the hour, so whatever you say boss"

I now consider myself very lucky to be successful enough to not have to deal with projects like that, but back when I wasn't I paid quite a few things I wanted with those sort of stupid decisions.

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

#36
post #3

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

If you could do this with a proper regex: they’re easy to parse and don’t have a lot of structure, so you can do neat things like produce generators from them. Now, you can get your regex for regexes to produce new regexes! This can be useful if you want to do generative testing.

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

#37

This is perfect example of a insight which leads to a rabbit hole of intertwined complexity of our concepts when you try to understand why regexp for validating regexp is actually much simpler than regexp for validating email address.

It depends what you mean by "validating email addresses".

A regex that tests if a string looks like a valid email address is simple. Forget the ancient RFCs, a real world email address is in the form of `mailbox@domainname`. Which is not so difficult to test for with a bit of care.

However, testing if the email address is a valid mailbox is harder and indeed impossible using regex alone. The domain name can be validated using standard domain tools but in practice the mailbox can be anything that the server will route to a valid mailbox. The only way to validate it is to send an email.

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

#38
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.

If you were looking for the reason why a regex cannot parse HTML, it is because HTML has matching nested tags and regex parsers are finite state machines (FSM).

What this means is that a regex parser is like a goldfish. It only knows about the state it is currently in (what it just read) and which possible states it may transition to (what is legally allowed to come next). The fish never remembers where it was before; there is no option to have the legality of a transition depend on what it read _before_ the current state. But this memory function is a requirement to recursively match opening tags to their closing tags - you need to keep a stack of opening stacks somewhere in order to then cross off their closing tags in reverse order. So regex cannot parse HTML.

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

#39
post #19
post #17

Earlier quoted context omitted.

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.

It is helpful. It may not provide the answer the OP wanted, but it provides the answer they needed.
Post reply on HN