I'd like to improve my regex knowledge. Are there any exercises/tools that you wolud recommend?
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
I highly recommend reading Mastering Regular Expressions. It lives up to its name.
Perl + Roberts Perl Tutorial are the source of most of my regex ability.
regular-expressions.info is an excellent site, and RegexBuddy is a great visualizing tool.
As Joe90 mentions, reading about them takes you a long way ;)
Personally I find http://www.regular-expressions.info/ a great resource for regex.
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.
Besides those already mentioned, you can try the Regex Coach. I've heard good things about it.
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.
Tutorials:
http://www.regular-expressions.info/tutorial.html - One great tutorial on regex
Online Testers:
http://osteele.com/tools/reanimator/ - it animates your regex as an automaton, cool!
http://regexpal.com/ - really cool and easy to use
http://www.fileformat.info/tool/regex.htm
Tools:
http://www.radsoftware.com.au/regexdesigner/
.Net specific:
http://msdn.microsoft.com/en-us/library/az24scfc(VS.71).aspx
Java specific:
http://java.sun.com/docs/books/tutorial/essential/regex/
GeneXus X specific:
http://wiki.gxtechnical.com/commwiki/servlet/hwikibypageid?4606
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).
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)
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.
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.
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/
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.
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.
You can never master regular expressions. Just when you think you have you realise it is the regular expressions who have mastered you.
Fix bugs in Jeff Friedl's book before he does. Until you do that, you haven't mastered regular expressions.
If you are working in .NET I recommend this site to visualize your results as well as see how to set up your code.
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.
http://www.gskinner.com/RegExr/
is something i found after looking through some other sites