4 ms·
A lot of regex-based solutions are not robust against bad input. Bad input doesn't always mean that the regexes don't match! Regexes have a very happy home in
by kazinator 6y ago
A lot of regex-based solutions are not robust against bad input.
Bad input doesn't always mean that the regexes don't match!
Regexes have a very happy home inside lexical analyzers for tokenizing languages. Lexers define what is correct input and match every case with some regex pattern. If there is no match, then the input doesn't contain a valid token: the lexer can loudly complain (logging an error that can be treated as fatal by the overall compile job), drop an input character and try matching again.
The typical regex solutions in Perl (Awk, ...) scripts go like this: "look for this flimsy, minimal regex somewhere in the stream and assume it's the right thing, then match this other regex in the same line and---woo hoo!--that's our item. Oh, false positives, schmozitives."
Basically, if we were to pin it down to a single difference in principles is that using regexes for searching for something small in something large is different from matching an entire input in its totality (and then getting at the desired parts).
Searching is the quick and dirty thing that's easy to reach for.