tags:

views:

2005

answers:

25

I'd like to improve my regex knowledge. Are there any exercises/tools that you wolud recommend?

+2  A: 

Reading about them is fine, but actually getting hands on experience with a tool is really helpful.

Try the Regulator - http://weblogs.asp.net/rosherove/pages/tools-and-frameworks-by-roy-osherove.aspx

Joe R
+11  A: 

I highly recommend reading Mastering Regular Expressions. It lives up to its name.

Haacked
This book was the first thing that came to my mind.
J.J.
@J.J. Same here. That is an excellent book
Eric Haskins
Nice link to anything but the book you mention. Besides, this answer was given five minutes prior to yours and the link to the book is correct.
TomC
Seems like you can't hear about this subject without having the owl mentioned.
abyx
Downvoted for stealth hyperlinking to your blog instead of the book you reference.
Michael Carman
What's up with the link?
mattruma
That's awesome. I didn't realize that there's some web/SO etiquette where you shouldn't link to your own site. If you can't shamelessly self-promote on this interweb, what good is it?
MrBoJangles
At least link to the book in question. Your blog entry has nothing to do with the book.
Andy Lester
Fixed the link in question. It was a clipboard mistake on my part. Sorry!
Haacked
+9  A: 

Start using RegExBuddy!

Galwegian
+14  A: 

Here are some good resources:

http://regexlib.com/

http://www.regular-expressions.info/

MrBoJangles
regexlib is the way to go!
Kon
Yeah, I just snagged an email validator expression yesterday. Thanks, anonymous contributor of said expression!
MrBoJangles
I like www.regular-expressions.info it is great. Every once in a while when I want to use a more obscure expression I refer to this site....
Cervo
+2  A: 

Perl + Roberts Perl Tutorial are the source of most of my regex ability.

Paul Nathan
+12  A: 

regular-expressions.info is an excellent site, and RegexBuddy is a great visualizing tool.

Forgotten Semicolon
+4  A: 

As Joe90 mentions, reading about them takes you a long way ;)

Personally I find http://www.regular-expressions.info/ a great resource for regex.

thmsn
+4  A: 

Practice, practice, practice. I've always found Perl's manual page on regular expressions to be an excellent reference for all the common and POSIX regex symbols and commands.

Gaurav
Also try http://perldoc.perl.org/perlretut.html
Brad Gilbert
+4  A: 

Besides those already mentioned, you can try the Regex Coach. I've heard good things about it.

simon
+6  A: 

To master them, don't stop at reading about them. Go down the list of posts tagged regex and start solving the problems. Don't peek at the answers until you're ready.

+29  A: 
warren
lol, i clicked this topic just to post the link to this book.. too fitting to the question :P
Simucal
That picture is begging for a macro/'shop.
Rob Howard
+4  A: 

Now we've given lots of pointers for learning more about regular expressions, it's probably worth quoting Jamie Zawinski:

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

Part of learning about regular expressions is learning when not to use them (and when they're appropriate).

simon
It's cute to say that, but there are cases where using a Regular Expression really is the correct answer - especially in simpler use cases where the regular expression is fairly understandable.
John Fiala
Excellent. I have adopted this as a heuristic, or rule of thumb if you don't like the pedantic sound of the word 'heuristic'.
MrBoJangles
John, I thought it was obvious (expanded now) that I wasn't saying you should *never* use them. There are times when they are just the right tool. Unfortunately, that's also a smallish subset of the times people try them.
simon
Yeah, you're misunderstanding the quote. I believe he said that during an anti-perl rant (possibly because it was difficult back in 97 to initiate a regex function in perl). He wasn't saying don't use it.
Keng
+4  A: 

As simon pointed out. The most important part in mastering Regex is when NOT to use regex but that skill will come eventually. Till then use regex when you're doing Word finds or in your fave texteditor or in Excel. Anywhere that gives you that option, take it till you get a good handle on it.

Also, learn about these concepts fairly early on.

  • greedy
  • lazy
  • what the difference between "." and "\w"
  • "\b"
  • the short-hand character classes ("\s", "\d", etc)
Keng
+5  A: 

I keep a copy of the Regular Expression Pocket Reference at hand while I work - it gives me the short info I need about the use of RegEx in about a dozen languages at hand.

John Fiala
This appears to be out of print.
Clay Nichols
The next edition is in print, at http://oreilly.com/catalog/9780596514273/index.html.
John Fiala
+8  A: 

Best way to master regular expressions is to use them in your day to day work. Use a regex aware editor. I recommend NotePad++. Use the find and replace functionality with regex enabled.

You need to know just the basics to start off. Words, characters, numbers, white spaces. They are quite easy to remember. You don't get everything right the first time. But you will learn every time you try it.

Remember, unless you use it regularly, you will probably forget regex in couple of weeks.

Vivek
+5  A: 

Expresso is a pretty good, free RegEx utility:

http://www.ultrapico.com/Expresso.htm

And a Regular Expressions Cheat Sheet which comes in handy:

http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/

Gordon Bell
Gordon, those are both excellent suggestions. I wish I could vote twice :-)
Clay Nichols
+2  A: 

I learned about them by learning Perl, and using it to create a configuration file parser. If you wanted to do something similar these days I would suggest Ruby, which has a similar hook for RE in it. Once I learned the basics, I found many of the tools I'd been using had great RE support, if you knew to use it. It is really very powerful.

I highly recommend the Regular Expressions Pocket Reference that others have mentioned. It will be much simpler to keep around to look up something quickly, as you will need to do. Not to mention that many applications have slightly different ways of implementing some parts of it (ie, whether you should have to backslash escape parenthesis).

If you do much text processing, I recommend learning RE. You will appreciate it.

MattG
+2  A: 

Make sure you understand the basics. You can get this from a variety of sources (depending on your choice of regex flavor).

You can play around with them on something like this.

Jeremy Cantrell
+2  A: 

You can never master regular expressions. Just when you think you have you realise it is the regular expressions who have mastered you.

Craig
Not to mention the fact that Perl's RegExs keep getting new, useful features.
Brad Gilbert
In Mother Russia, regular expressions master You!
MrBoJangles
+1  A: 

Fix bugs in Jeff Friedl's book before he does. Until you do that, you haven't mastered regular expressions.

Windows programmer
+2  A: 
Rob Howard
Redundant answers is not a problem.
Goran
Is each answer accepted 0 or more times, or 1 or more times?
Windows programmer
Subtle 'shop not as funny as originally hoped. D:
Rob Howard
A: 

If you are working in .NET I recommend this site to visualize your results as well as see how to set up your code.

A: 

I found that learning about how they were implemented under the hood really helped. Actually it was studying languages and compiler design that I first encountered regexes. Knowing where they come from, and how they relate to grammars, DFAs, lexers, etc I think is a real help. It especially helps with debugging, since you can get into the mind of the regex so to speak.

Draemon
A: 

http://www.gskinner.com/RegExr/

is something i found after looking through some other sites

CheeseConQueso