6 ms·
If you're using a regex it's a smell that there is a better tool for what you need to do. A ton of if statements is a much more readable way of writing code tha
by Shindi 4y ago
If you're using a regex it's a smell that there is a better tool for what you need to do. A ton of if statements is a much more readable way of writing code than regex.
- encryptluks2 4y agoUnfortunately regex is the best we have. A bunch of if statements is also a whole less efficient and prone to errors than a regex statement. Once you start to learn regex, it actually isn't that bad. The challenging part is that a lot of people cut and paste not really understanding it and you end up with a bunch of bad regex examples.
- jwestbury 4y agoEh, I think the challenging part is understanding a complex regex that's already been written. You really do need good comments and unit tests to go along with a regex in order to understand the intent, otherwise they rapidly become unmaintainable.
- zamnos 4y agoRespectfully, no. If you're trying to write a full blown language parser then regexps are the wrong tool, but a 50 line function doing the job of a regexp has a really funky code smell. Comment your regexps*, and use a parser when your usecase outgrows regexps, but a blanket ban on regexps smells of "I'm not smart enough to understand them". * https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch02s18.html https://www.oreilly.com/library/view/regular-expressions-coo...