News for hackers. Stuff that matters.
Re:Is there a regular expression to detect a valid regular e... by shakna on Sunday September 08, 2019 @10:02AM (#20910600)
A parser. Specifically, an XHTML parser.
Re:Is there a regular expression to detect a valid regular e... by unlinkr on Sunday September 08, 2019 @10:33AM (#20910794)
How do you think an XHTML parser is written? In particular, how does an XHTML parser identify tokens like start and end tags?
Re:Is there a regular expression to detect a valid regular e... by journalctl on Sunday September 08, 2019 @12:07PM (#20911350)
With a pushdown automaton [1] or something like a linear bounded automaton [2].

[1] https://en.m.wikipedia.org/wiki/Pushdown_automaton

[2] https://en.m.wikipedia.org/wiki/Linear_bounded_automaton

More specifically, a stack lets you keep track of nesting. See an opening tag, push something onto a stack. See a closing tag, pop the stack. If the stack is empty at the end, the tags match.

Parsing XHTML in real life is of course much more complicated than this, but this is the basic idea.

Re:Is there a regular expression to detect a valid regular e... by unlinkr on Sunday September 08, 2019 @01:30PM (#20911932)
I think there are a lot of knee-jerk answer because people see "XHTML" and "regex" in the same sentence and immediately think "not possible".

But the actual question is clearly not about matching start tags to end tags or building DOM or anything like that - which indeed would require a stack. The question is about recognizing start and end tags. You can do that perfectly fine with regular expressions - indeed many parsers uses regular expressions to tokenize the input before parsing.

Furthermore, the question specifically needs to recognize the difference between start-tags and self-closing tags. A differece which is not exposed by most XHTML parsers a far as I am aware

Re:Is there a regular expression to detect a valid regular e... by journalctl on Sunday September 08, 2019 @02:43PM (#20912472)
Sorry, I misread. Indeed, actually tokenizing text is accomplished with regular expressions (although some parsers don’t need a tokenization pass, but details :).
Re:Is there a regular expression to detect a valid regular e... by saagarjha on Sunday September 08, 2019 @11:44AM (#20911196)
It keeps track of state that a regular expression cannot?
Re:Is there a regular expression to detect a valid regular e... by unlinkr on Sunday September 08, 2019 @01:16PM (#20911833)
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 e... by shakna on Monday September 09, 2019 @02:42AM (#20915735)
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 <a attr="<b>"> which may well be valid input.)
Re:Is there a regular expression to detect a valid regular e... by unlinkr on Monday September 09, 2019 @03:47AM (#20916032)
So how would you tokenize without the use of regular expressions? What more appropriate technique would you use instead?

The example you provide in not XHTML so not really relevant for the discussion. But in any case, a regular expression have no problem recognizing a quoted string.

Re:Is there a regular expression to detect a valid regular e... by shakna on Monday September 09, 2019 @04:25AM (#20916196)
> So how would you tokenize without the use of regular expressions?

Since this need doesn't appear to be an everyday one, with clearly defined targets, a simple hand-written lexer isn't hard to write, and will make less mistakes than a regex. Just use a scanning approach. As a bonus, you'll still be able to read in 12 months time.

Re:Is there a regular expression to detect a valid regular e... by unlinkr on Monday September 09, 2019 @04:48AM (#20916309)
Why would a hand-written lexer have fewer mistakes than a regular expression using an off-the-shelf regex engine? They would need to encode the same lexical grammar, so at that level there is the same amount of complexity.

Writing a lexer by hand is just trading ten lines of regex (a widely known declarative DSL) with hundreds of lines of custom code. I don't see how that would be more maintainable in the long run.