3 ms·
Easy... (Qadaffi|Khadafy|Qadafi|...)... it's self-documented, maintainable, and assuming your regexp engine is actually a regexp engine, will compile to the sam
by colanderman 16y ago
Easy... (Qadaffi|Khadafy|Qadafi|...)... it's self-documented, maintainable, and assuming your regexp engine is actually a regexp engine, will compile to the same DFA that the obfuscated garbage in the SO solutions will compile to.
Writing compact regular expressions is like using short variable names to speed up a program. It only helps if your compiler is brain-dead.
- true_religion 16y ago> assuming your regexp engine is actually a regexp engine There is no guarantee that two regex that match the same corpus will compile to the same DFA or even compile to a DFA at all. Do not confuse regex with a regular expression. The answer isn't to turn against any of these variation of regex by deriding them as "compact" but instead test to see if this is a bottle neck, and try out a non-self documenting variation if it is.
- colanderman 16y agoYou are right, I should be more precise with my terminology. By "regexp" I meant "regular expression". Regardless, neither term implies implementation as a DFA, however regular expressions, and the subset of regexps which are equivalent to them, can and should be compiled to an optimized DFA.
- Peaker 16y agoA cartesian multiplication is not necessarily more readable, if it takes exponentially more code. A bit more verbosity over [QKG]add?... could be nice. For example, Haskell's parsec looks like: do oneOf "QKG" string "ad" optional (char 'd') .. This also has the nice benefit of generating nice errors when it fails ("Expected 'ad', found ...").