4 ms·
> Regexes are almost always a massive code smell. They should almost never be used Regular expressions are just a notation for expressing a finite state automa
by gnuvince 10y ago
> Regexes are almost always a massive code smell. They should almost never be used
Regular expressions are just a notation for expressing a finite state automata that recognizes a regular language: if that's the problem that you have, regular expressions are definitely the tool you want to be using. For instance, I recently made myself a small tool to compute the min/max/expected value of a dice roll; the notation for such a thing forms a regular language that can be expressed as follows:
non_zero_digit ::= '1' | ... | '9'
digit ::= '0' | non_zero_digit
integer ::= non_zero_digit digit*
modifier ::= '+' integer | '-' integer
roll ::= integer 'd' integer modifier?
Converting this grammar to a regular expression is straight-forward and was the correct tool to use.
I agree that regular expressions are often used in contexts where they should not, especially when people start using back-references to recognize non-regular languages, but don't throw out a perfectly good tool because sometimes it is ill-suited.