Mastering Regular Expressions
5.1. Regex Balancing Act
Writing a good regex involves striking a balance among several concerns:
These concerns are often context-dependent. If I'm working on the command line and just want to grep something quickly, I probably don't care if I match a bit more than I need, and I won't usually be too concerned to craft just the right regex for it. I'll allow myself to be sloppy in the interest of time, since I can quickly peruse the output for what I want. However, when I'm working on an important program, it's worth the time and effort to get it right: a complex regular expression is OK if that's what it takes. There is a balance among all these issues. Efficiency is context-dependent, even in a program. For example, with an NFA, something long like |