Live data from Hacker News

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

stackoverflow.com

121–130 of 224 posts

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

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

The simplest explanation is because you already have a lot of functionality implemented this way.

Still probably the library you are using exposes a validation function...

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

#122

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?

As typically defined academically, yes. In the real world things might be more complex, but I think it would hold true for many implementations.

Academically a regex is a string of of:

- character literals

- "epsilon" characters (meaning empty string)

- "+" characters (which means either what we see before the + or what we see after the +, in real-world regex these are usually represented as | instead of + as well as used implicitly in constructs like [a-zA-Z])

- "*" characters (same meaning as in real-world regex)

- And parentheses to allow controlling the order of operations

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

#123

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?

[deleted]

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

#124
post #2

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

From that answer: "You can't parse [X]HTML with regex".

I've always wondered how that came about. How come very early in the development of the web no one important enough for people to pay attention to them said, "Hey...wait a second. If this thing becomes popular, people are going to really want to processes web documents with their usual text file processing tools and techniques. We really outta make this thing reasonably easy to process with grep and sed and awk and Perl and such"?

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

#125
post #82
post #73

Earlier quoted context omitted.

The big problem with the content is suggesting to use an XML parser to parse HTML. That might often work, and several XML parsers have some sort of HTML "mode", but in general, no, not really. HTML documents are frequently invalid XML. HTML documents lack an XML declaration, they don't close all tags, etc. They might even lack a root element. Someone mentioned this in a comment on SO, but it was (as far as I could se…

The question title implies he's parsing XHTML text. > XHTML is an XML-based HTML. It serves the same function as HTML, but with the same rules as XML documents. https://stackoverflow.com/q/1429065

The question title is ambiguous but the question tags include both HTML and XHTML. Self-closing `` is an XML-ism which in HTML is treated the same as `` but it was still fashionable to use them in HTML when XHTML was at its height.

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

#126

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

The problem is that "regular expression" is equivocal.

Sometimes you hear "regular expression" used to mean the language of things accepted by some Finite State machine. Lots of folks on HN took a Theory of Computation class and learned about these in that class. In that meaning, yes, you are right.

But sometimes in professional conversations you hear "regular expression" used to mean the strings that can be matched using, say, Perl's regular expressions. I know I have heard it used that way often. Since Perl regular expressions can accept anything a Turing machine can accept, the answer to the question in this case is yes.

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

#128
post #71

Earlier quoted context omitted.

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

It keeps track of state that a regular expression cannot?

You don't need to keep track of state to match tokens like XHTML start or end tags.

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

#129
post #17

Earlier quoted context omitted.

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

it's sad that today the stack overflow majority shares your mindset and an answer like that would drown in downvotes or killed by moderation. some people like to act super serious all the time like they're playing a sitcom version of what they think adulthood is in a quest to be the most boring person on earth like if that's the goal of human interaction

You can be funny and helpful at the same time.
Post reply on HN