Live data from Hacker News

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

stackoverflow.com

191–200 of 224 posts

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

#191

Earlier quoted context omitted.

Just considering the language of balanced parentheses, the only memory needed is a single integer to track depth. If we see a "(", increment. If we see a ")", decrement. If we never go negative and end up with zero, that's a balanced parentheses expression. (To be clear, the parser I've described is not a regex parser.) Adding the rest of the regex machinery makes this a bit more complex, but in general, if the compu…

)))((()(()

"never go negative"

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

#192

Earlier quoted context omitted.

Just considering the language of balanced parentheses, the only memory needed is a single integer to track depth. If we see a "(", increment. If we see a ")", decrement. If we never go negative and end up with zero, that's a balanced parentheses expression. (To be clear, the parser I've described is not a regex parser.) Adding the rest of the regex machinery makes this a bit more complex, but in general, if the compu…

This only works if you have a single type of parenthesis. Is that applicable to regex expressions in general that only ()’s can nest?

You could have a stack of (paren-type, integer) pairs. If the paren has a different type than the top pair on the stack (or the stack is empty), push a new pair with the integer set to one. If the integer goes to zero, pop the pair. At the end of the expression, you have to have an empty stack.

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

#193
post #107
post #77

Earlier quoted context omitted.

Can regex engines actually process arbitrarily deep nestings themselves though? It seems to me it would start getting computationally expensive very quickly to search for those kinds of regexes, so is 'arbitrarily deep' a practical concern for todays hardware?

Yes, regex engines that are designed to do so run in linear time on the size of the regex (as well as linear time with respect to the length of the input). One such engine is rust's https://github.com/rust-lang/regex

> One such engine is rust's https://github.com/rust-lang/regex

It can process arbitrary deep nestings? I'm not familiar with it, but I had a look and I can't see anything in there that would allow it to do it. Can you point to syntax that allows it?

In general regex's that run in linear time are based on DFA's (as opposed to NFA's - which is what most use).

A DFA is part of the Chomsky hierarchy of languages that has DFA's at the bottom (aka regex's, least powerful - can't handle recursive structures), a DFA coupled with an infinite stack for memory (aka LR parsers, can't handle context sensitive grammars, also runs in very close to linear time), and finally a DFA coupled with an infinite tape (aka Turing Machine, can do any sort of computation).

In each of these cases the DFA (regex) is essentially a computer program and all they are doing is allowing it to store stuff on various kinds of memory (none, stack, tape with arbitrary movements). An interesting corollary of this is any computer program can be expressed as a DFA.

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

#194
post #160

Earlier quoted context omitted.

That is not XHTML.

What about this --> -->?

Yes you can tokenize this with a regular expression and extract the valid start and end tags.

If comments in XHTML could nest you would have a problem. But this is not the case.

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

#195
post #51
post #17

Earlier quoted context omitted.

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

I found it extremely helpful. The sheer emphasis of the reply made me very curious why the idea of using regex on xml is so bonkers. - I will never forget that regex can't parse XHTML - the reason being, regex is insufficiently powerful - when I first saw this post, I knew little about regex under the hood, this sent me down a wiki hole of FSMs, pushdown automata and turing machines - this misconception is apparently…

The problem with the answer is it is wrong. The question is about identifying start-tags in XHTML. This is a question of tokenization and can be solved with a regular expression. Indeed, most parsers use regular expressions for the tokenization stage. It is exactly the right tool for the job!

Furthermore, the asker specifically needs to distinguish between start tags and self-closing start tags. This is a token-level difference which is typically not exposed by XHTML parsers. So saying "use a parser" is less than helpful.

I have elaborated a bit in blog post: https://www.cargocultcode.com/solving-the-zalgo-regex/

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

#196
post #71
post #54

Earlier quoted context omitted.

A parser. Specifically, an XHTML parser.

How do you think an XHTML parser is written? In particular, how does an XHTML parser identify tokens like start and end tags?

If I were writing a limited parser, in answer to the narrow question being asked, I wouldn't be using regex at all. It's not suited to this particular problem. (For example it would get caught on things like "> which may well be valid input.)

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

#197
post #91

Earlier quoted context omitted.

It depends on the representation. If the representation is in the FSA format (state, character, state), it becomes nearly trivial.

An analogous problem for this representation would be detecting transitions to undefined states, or ambiguous (duplicate) transitions.

I didn't verify it, but my intuition says that can't be done with a CFG; it seems at least context-sensitive.

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

#198

TL;DR: No. But there exist languages built on top of regular expressions (notably PCRE) that can. They can't validate themselves , though - turtles all the way down to Gödel.

Plenty of parser formalisms can define their own syntax in their own syntax. As mentioned elsewhere, C compilers can parse their own source code. Gödel has nothing to do with it.

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

#199

Earlier quoted context omitted.

How about this for ease-of-use? sentence --> noun_phrase, verb_phrase. noun_phrase --> det, noun. verb_phrase --> verb, noun_phrase. det --> [the]. det --> [a]. noun --> [cat]. noun --> [bat]. verb --> [eats]. Reads -and behaves- as BNF. Strings in []'s are terminals, the rest are nonterminals. It's directly executable as a logic program (it's Prolog syntactic sugar).

That looks great, but it's hard to position it as a regex competitor. I should be more explicit about what I mean by "regex-style": The cool thing about regex that makes them so approachable is that they kind of look like the thing they're describing/matching. Your thing here mostly does not.

"My thing here" is a grammar so it describes a class of strings. Perhaps this is why it looks to you unlike what it describes?

You can make a DCG rule as specific or as general as you like. As a for instance, this is a vim regex I retrieved from my recent history:

  \[13\/13,15\/12,24-24]
You could write this like so in DCG notation:

  s --> ['[',13,'/',13,,,15,'/',12,,,24,-,24,']']. 
And that would match the string "[13/13,15/12,24-24]", no less, no more.

DCGs are Turing-complete, so you can go all the way from programs to finite automata when you write a pattern to match.

They're not really a regex competitor. They were invented in the '70s as a formalism for context-free grammars to be used in representing natural language. They fit right into the logic programming language Prolog that was created soon after (and by some of the same people).

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

#200
post #70
post #44

Earlier quoted context omitted.

Here is the answer to the question: https://www.cargocultcode.com/solving-the-zalgo-regex/ tl;dr: It can indeed be solved relatively easily with a regex.

It doesn't work for me with regex101. "The preceding token is not quantifiable" on this part: |

It was supposed to be (? \w+ ) in order to create a named capture. The was apparently lost in editing. Thanks for the heads-up.
Post reply on HN