4 ms·
i'm a huge regex nerd (see: regexpixie.com). But this is a classic example of people trying to be clever instead of clear. And I would always much prefer to rea
by viggity 10y ago
i'm a huge regex nerd (see: regexpixie.com). But this is a classic example of people trying to be clever instead of clear. And I would always much prefer to read clear code than clever code.
Regexes are declarative and they're really good a narrow set of features - namely, finding text that matches a pattern. When I see someone using negative/positive lookaheads, or trying to match a valid date with a regex, I see someone who is furiously pounding a nail with the fat end of a screwdriver.
If you want to keep things simple with regular expressions:
* Be liberal with what your pattern matches and use a normal programming language for your complicated conditional logic to filter out crap you don't want
* Don't be afraid to break up the search with multiple regular expressions
* Ignore pattern whitespace and use it to visually break up your pattern. Nobody would agree to debug javascript that has been minimized, yet people do this all the time with regex
* For the love of all that is holy, USE NAMED GROUPS. It is a fantastic way to document your intent.
This is confusing: \d{1,2}/\d{1,2}/(\d{4}|\d{2})
This is not:
(?<day>\d{1,2})
/
(?<month>\d{1,2})
/
(?<year>\d{4}|\d{2})
- rntz 10y agoThis isn't about negative lookaheads or lookbehinds. It's about negation proper. It is perfectly declarative: the language matched by (?~E) is the complement of the language matched by E.
- brudgers 10y agoI've found trying to point out the differences between regular expressions as finite automata and regular expressions as a reference to the regex|regexp constructs within programming languages difficult, and have given it up as a hobby. The programming language constructs are useful in many cases but harder to reason about. The finite automata are easier to reason about but limit themselves to state machines. Here the complexity is that the operator operates in the state-machine space wrapped up inside the programming language construct. Ok, so I guess I did not give it up as a hobby.
- petercooper 10y agoHave you considered doing a sort of regex best practices post? I'd be happy to promote such a thing or even help get it out. I think a lot of people want that sort of info.
- falsedan 10y agoIf I wrote a post, it would be: "read Ch. 12 of Perl Best Practices (2005)[0]". Yow, $30+ for the ebook! I wouldn't buy if just for that chapter… fortunately, the guidelines from the book have been codified as Perl::Critic, so If you don't mind missing out on the gently guiding text which accompanies each rule, you can browse the Perl::Critic::Policy::RegularExpressions[1] submodule docs. [0]: http://shop.oreilly.com/product/9780596001735.do http://shop.oreilly.com/product/9780596001735.do [1]: https://metacpan.org/pod/Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes https://metacpan.org/pod/Perl::Critic::Policy::RegularExpres...