Live data from Hacker News

Stop avoiding regular expressions damn it

bradt.ca

41–50 of 55 posts

Re: Stop avoiding regular expressions damn it

#41
OT: Where from come this seemingly odd and new habit of spacing inside parentheses? I always write "(a, b)", mostly because it is closer to English (or other languages) typography, and it seem to have good readability, plus it is, I believe, the standard in most languages. So why write "( a, b )"?

By the way, if some like spacing that much, and if the reason is to have a better mouse-selectability, then I humbly propose "( a , b )".

Re: Stop avoiding regular expressions damn it

#43

Stop propagating bad interfaces like 'regular expressions' damn it! An interface that e.g. makes me 'escape' half of my input because its designers think their special use of characters must take precedence over all user input is a bad interface.

It's not an interface, it's a language. Most (all?) languages have collisions between in-band and out-of-band information and ways to "escape" them.

Re: Stop avoiding regular expressions damn it

#44
post #41

OT: Where from come this seemingly odd and new habit of spacing inside parentheses? I always write "(a, b)", mostly because it is closer to English (or other languages) typography, and it seem to have good readability, plus it is, I believe, the standard in most languages. So why write "( a, b )"? By the way, if some like spacing that much, and if the reason is to have a better mouse-selectability, then I humbly prop…

It's WordPress' PHP Coding Standards: http://make.wordpress.org/core/handbook/coding-standards/php...

Re: Stop avoiding regular expressions damn it

#45
As a general rule I ask people to avoid using non-trivial regular expressions. The grammar is too tricky and often the expression doesn't mean what the developer intends it to mean. Or the next developer will make a mistake.

My current pet peeve is with parser combinators, wich seems a good compromise (it's not a magic wand) between maintenance (whereas external parser generators don't blend well in your code), parsing what you think you are parsing (more so when your grammar was defined with rules in a reference document), and integrating the parser with your code.

Re: Stop avoiding regular expressions damn it

#46
post #4

THE single best ressource to really learn how to deal competently with regex is still Jeffrey Friedl's book "Mastering Regular Expressions". You will profit from it for the rest of your career. (There's also a Regex short reference and a Regex cookbook by O'Reilly...)

Sincere question - is it worth investing time into reading a 500 odd page book for something that I might not use that frequently in my career? From my experience, I've seen that I can get away by just Googling or just experimenting whenever I'm stuck on a regex.

I guess your question is why read a book when you can just learn as you go and as you need to. I didn't read a book, but I probably should have because it took a long time for me to pick up things that would have helped a ton earlier on. For example, I recently learned that you can turn off "greedy" when using .* by adding a ? after it. This was a huge revelation that I would have benefited from day one, ten years prior.

Re: Stop avoiding regular expressions damn it

#47

The core criticism of regular expressions is legitimately directed at intermediate programmers who know enough to be dangerous, but is sometimes inappropriately cargo-culted by beginner programmers who use it as an excuse not to learn regular expressions. The fact is that despite pithy slogans, there is a sweet spot where a regular expression does the job of matching a string in a clearer fashion than anything else.…

Though there's nothing wrong with busting out a baroque regex in one time use contexts (editor's search function, throw-away application of grep or sed, &c).

Of course not.

Re: Stop avoiding regular expressions damn it

#48
post #4

THE single best ressource to really learn how to deal competently with regex is still Jeffrey Friedl's book "Mastering Regular Expressions". You will profit from it for the rest of your career. (There's also a Regex short reference and a Regex cookbook by O'Reilly...)

Sincere question - is it worth investing time into reading a 500 odd page book for something that I might not use that frequently in my career? From my experience, I've seen that I can get away by just Googling or just experimenting whenever I'm stuck on a regex.

Well, maybe not an entire 500 page book, though I'm sure it wouldn't hurt. You could always try Zed Shaw's the-hard-way book on the subject: http://regex.learncodethehardway.org/book/ (haven't read it but the the-hard-way books seem to be fairly well regarded)

I do take issue with your suggestion that you might not use this stuff all that frequently in your career. This is definitely at odds with my experience. Even though I don't use them that much in final-quality code, I use them all the time from the text editor, and quite often for quick one-off text manipulation or extraction scripts. Having a quick way to extract text from ad-hoc data can quickly get you a rough answer to a speculative question, the text equivalent of of back-of-the-envelope calculation, without needing to do a lot of work and without needing the question to justify a lot of work.

But I mainly use them for searching for one of two or three different strings in the text editor.

Re: Stop avoiding regular expressions damn it

#50

More concise? Sometimes. Slower? Always. BenchmarkRegexp 500000 5136 ns/op BenchmarkStrings 10000000 173 ns/op http://play.golang.org/p/YT29Ao-tOt

Using Golang as an example of real world regex performance is borderline dishonest. Their regex engine is notoriously unoptimized and is not intended to be a strong point of the language.
Post reply on HN