Live data from Hacker News

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

stackoverflow.com

111–120 of 224 posts

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

#112
post #45

Earlier quoted context omitted.

The question is about identifying end-tags in XHTML. This is indeed possible with a regex.

theoretically I believe an end tag really requires a valid start tag. anyway you can probably answer any number of simple questions about a bit of HTML using regex but as code wants to grow to handle more use cases there will come a time when the solution will break down and the code that wrote to handle all the previous uses will need to be rewritten using something other than regex.

An element requires a start and end tag, or a self-closing start tag.

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

#113
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?

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?

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

#114
post #91

> Is there a regular expression to detect a valid regular expression? No, there is not. For example, parentheses in a regex must be balanced, and (famously) there is no regex to detect balanced parentheses.

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

[deleted]

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

#115
post #104

Earlier quoted context omitted.

Are you sure you aren't mistaken? :-)

Go ahead - show me wrong.

I have already done that, what I am unsure of is how to go about convincing you.

With compilers - I just fix the bug myself. With humans, I have to convince the human to self-correct.

Common problem in distributed systems that - leader election.

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

#117
post #94
post #88

Earlier quoted context omitted.

You are axiomatically assuming that the proposition "V determines well-formedness of any regular expressions" to be true. I am asking you to prove that. Constructively.

:-)

So we agree then that when the system says "I am free of errors", some (most?) of the time it's a lie ? ;)

All models are wrong - some are useful.

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

#119
post #69

So, given the much discussed limitations of reg-exps and the desire to parse context-free grammars. My question is, why are we still using regular expressions. Or rather, why isn't there something as easy to use as regular expressions that can processes context-free grammars?

Something like Perl6 grammars[1], or maybe Rosie Pattern Language[2]? Of course Perl6 regexes also go well beyond regular expressions, and I suspect they could be used to match context-free grammars if pressed hard enough. Both P6 grammars and RPL are based on parsing expression grammars, and there are also tools/libraries for many other languages based on PEGs. But now you are entering in the scary realm of parsers and parser generators and all that jazz, and can debate how easy to use they really are.

[1] https://docs.perl6.org/language/grammars

[2] https://developer.ibm.com/open/projects/rosie-pattern-langua...

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

#120

Earlier quoted context omitted.

I don't know if it's possible to do the "string describing a set strings" thing that regex does with more powerful parsing while maintaining the ease of use. CFGs make heavy use of named sub-languages (productions). A regex-style parser would at least have to have some kind of recursion-inducing metacharacter.

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).

And this can be used to generate sentences as well as parse them. ;-)
Post reply on HN