News for hackers. Stuff that matters.
Re:Is there a regular expression to detect a valid regular e... by stfwn on Sunday September 08, 2019 @09:21AM (#20910420)
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 e... by inflatableDodo on Sunday September 08, 2019 @11:08AM (#20910981)
>What this means is that a regex parser is like a goldfish.

Like a drunk goldfish, or a sober goldfish?

'A method to study short-term memory (STM) in the goldfish.'

'Twenty-one common goldfish (13-15.5 cm long) were randomly divided into alcohol (A) and nonalcohol (NA) groups and were trained in an alcohol solution of 400 mg/100 ml or in water, respectively. All alcohol fish were placed in an alcohol solution of 400 mg/100 ml for 3 hr before training in the same alcohol concentration. Fish were trained on a position discrimination task for 2 consecutive days. The door used for training was that opposite to each fish's spontaneous preference. Savings in relearning on Day 2 was taken as a measure of long term memory strength. Only fish which reached criterion on both days were immediately given 10 forced reversal trails in the opposite direction (i.e., a fish trained on right door was forced to choose the left door.) A and NA subjects were then tested after a 5 min (STM) delay, respectively, in a free choice situation for 10 trails (i.e., neither door was blocked). The results suggest that alcohol facilitates the STM of the forced reversal information.'

https://www.ncbi.nlm.nih.gov/pubmed/935220

Re:Is there a regular expression to detect a valid regular e... by unlinkr on Sunday September 08, 2019 @09:54AM (#20910566)
The question is about identifying end-tags in XHTML. This is indeed possible with a regex.
Re:Is there a regular expression to detect a valid regular e... by nurettin on Sunday September 08, 2019 @11:23AM (#20911064)
<a href="</a>">try this</a>
Re:Is there a regular expression to detect a valid regular e... by unlinkr on Sunday September 08, 2019 @12:09PM (#20911365)
That is not XHTML.
Re:Is there a regular expression to detect a valid regular e... by hk__2 on Sunday September 08, 2019 @03:05PM (#20912658)
What about <a>this <!-- </a> --> </a> <!-- </a> -->?
Re:Is there a regular expression to detect a valid regular e... by unlinkr on Monday September 09, 2019 @01:56AM (#20915540)
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 e... by hk__2 on Monday September 09, 2019 @03:53AM (#20916055)
> Yes you can tokenize this with a regular expression and extract the valid start and end tags.

So you need more than a regular expression, hence your premise is incorrect.

Re:Is there a regular expression to detect a valid regular e... by unlinkr on Monday September 09, 2019 @04:52AM (#20916334)
No, you don't need more than a regular expression. If you want to extract elements, i.e. match start tags to the corresponding end tags, then you need a stack-based parser. But just to extract the start tags (which is the question) a regular expression is sufficient.

The original question is a question about tokenization, not parsing, which is why a regular expression is sufficient.

Re:Is there a regular expression to detect a valid regular e... by nurettin on Sunday September 08, 2019 @12:26PM (#20911487)
<input value="how about this? />"/>
Re:Is there a regular expression to detect a valid regular e... by unlinkr on Sunday September 08, 2019 @01:15PM (#20911821)
That is a valid XHTML tag (if I remember correctly) and can be matched perfectly fine by a regex.
Re:Is there a regular expression to detect a valid regular e... by nurettin on Sunday September 08, 2019 @01:55PM (#20912108)
Perhaps something like "([^"]*)" could skip what is inside the string literal. Unless there is "<input" in the string literal, then where you start parsing becomes very important.
Re:Is there a regular expression to detect a valid regular e... by unlinkr on Monday September 09, 2019 @04:58AM (#20916371)
That pattern would indeed match a quoted string. I don't see how it would matter if the quoted string contains something like "<input". It can contain anything except a quote character.
Re:Is there a regular expression to detect a valid regular e... by nurettin on Monday September 09, 2019 @08:34AM (#20917502)
It just makes the starting offset of the regex input important.
Re:Is there a regular expression to detect a valid regular e... by unlinkr on Monday September 09, 2019 @09:28AM (#20917916)
Sure. But that would be true for any parsing technique. No parser known to man would be able to produce a valid parse if you start it in the middle of a quoted string!
Re:Is there a regular expression to detect a valid regular e... by bryanrasmussen on Sunday September 08, 2019 @10:31AM (#20910765)
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.

Re:Is there a regular expression to detect a valid regular e... by unlinkr on Monday September 09, 2019 @06:29AM (#20916856)
You have to distinguish between the different levels of parsing.

Regexes are appropriate for tokenization, which is the task of recognizing lexical units like start tags, end tags, comments and so on. The SO question is about selecting such tokens, so this can be solved with a regex.

If you have more complex use cases like matching start tags to end tags, you might need a proper parser on top. But you still need tokenization as a stage in that parser! I don't see what you would gain by using something other than regexes for tokenization? I guess in some extreme cases a hand written lexer could be more performant, but in the typical case a regex engine would probably be a lot faster than the alternatives and certainly more maintainable.

I know it is possible to write a parser without a clear tokenization/parsing separation - but it is not clear to me this would be beneficial in any way.

Re:Is there a regular expression to detect a valid regular e... by unlinkr on Sunday September 08, 2019 @12:11PM (#20911376)
An element requires a start and end tag, or a self-closing start tag.