Live data from Hacker News

Stop avoiding regular expressions damn it

bradt.ca

1–10 of 55 posts

Re: Stop avoiding regular expressions damn it

#3
post #2

What's a good resource for learning reg exp?

Great question. I learned them through osmosis over many years of looking at them in other people's code and tinkering myself. I don't remember ever going through a tutorial or reading a book. Probably not the best way to learn them as it definitely took a long time to have a good grip on them and I was missing important pieces for a long time. For example, it was only relatively recently that I learned that you can turn off "greedy" when using .* by adding a ? after it.

Re: Stop avoiding regular expressions damn it

#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...)

Re: Stop avoiding regular expressions damn it

#5
post #2

What's a good resource for learning reg exp?

http://www.regular-expressions.info/ is something I've stumbled across a few times in recent years, recommended by my friends to one another.

If I recall correctly I learnt the basics from MSDN documentation and later more thoroughly when I first came across Perl. Either of these are pretty decent choices, too. :)

Re: Stop avoiding regular expressions damn it

#6
post #2

What's a good resource for learning reg exp?

How about this site? http://www.regular-expressions.info/ I just skimmed it, and I'm not a regex pro, so I can't vouch for it's quality, but it seems like a place to start. For C#, I kinda enjoyed the dotnetperls page on regex http://www.dotnetperls.com/regex.

Re: Stop avoiding regular expressions damn it

#7
post #2

What's a good resource for learning reg exp?

What you want is to achieve a level where you can start thinking in regexp.

Eg: /^([a-z]*)$/ this translates into my brain to: a string that starts with a capture group of 0-N characters between a-z and also ends with it.

Then it's really easy.

Re: Stop avoiding regular expressions damn it

#8
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.

Re: Stop avoiding regular expressions damn it

#9
A little back story on this article for those who are interested...

I noticed my coworker was going out of his way to use string manipulation, writing many lines of code instead of a simple regular expression. When I asked why, he explained that he didn't know regular expressions, but more importantly that he felt that he had read a lot of posts on Stack Overflow discouraging use of regular expressions. From what he had read, he felt that it was better practice to avoid regular expressions. Although this could be anecdotal, there may be a real danger here that inexperienced programmers are getting the wrong message, that regular expressions are somehow bad in most situations and not worth learning.

Re: Stop avoiding regular expressions damn it

#10
post #2

What's a good resource for learning reg exp?

I find picking an actual context is really important to keep you motivated as learning about regexps can be tedious and prone to error. Probably the most fun regexp work I've done has been with Apache's mod_rewrite - using .htaccess files of course - because it's a great feeling to master something that useful that previously just seemed like magic.
Post reply on HN