views:

183

answers:

9

Possible Duplicates:
How do I learn Regular Expressions?
Learning Regular Expressions

I've read regex tutorials, books, stackoverflow questions. But I can't 'get' regex. I don't know why. But I always end up asking for help writing regexes. Has anyone discovered a good way to learn regex?

+1  A: 

Well, if books, tutorials, etc, have failed you, implement one. Keep it simple at first, and then add more sophisticated features.

If you can't understand after THAT... give up.

Just make sure you have plenty tests to go with it. In this particular case, I'm not sure I'd go with TDD. What you should do is create an automatic regexp generator, and test your implementation against the default implementation in your language.

It might help as well learn the DFA <-> Regular Expression thingy, and translation.

Daniel
Implementing a DFA/NFA is a good theoretical learning tool in general, but for learning how to actually use regexes on text, I can't see this as being too helpful.
Robert Fraser
@Robert Have you tried it? I did.
Daniel
Next quarter I will.
Robert Fraser
+1  A: 

Practice, Practice, Practice. I'm still learning.

No Refunds No Returns
+2  A: 

Download a IDE tool like this http://www.radsoftware.com.au/regexdesigner/ where you can put some text and specify and regex and keep experimenting it where you can get feedback instantly

Pick up a book or online material or real problem and try to construct it in that environment. Once you have applied it to a few real problems that you are facing, you will pick it up and remember it.

I have been programming for 10+ years too and only really learnt regex about 2 years ago.

Fadrian Sudaman
+2  A: 
  • Try RegexBuddy
  • Use regular expressions daily, even, at first on simple exercises / riddles not directly related to work
  • Once you become more fluent, and just like the one who's good with a hammer, understand that not everything is a nail, i.e. not all problems that deal with text-at-large should be solved with regex.
mjv
+1  A: 

One thing that helped me to learn regular expressions was to use a text editor with good, simple support (like Vim, Emacs, etc.).

In Vim, to do a regex search in your code, all you do is type "/", then enter your search regex. This way you can practice doing simple searches, and gradually build up to more complex ones. If you use it everyday, it will start to come together.

Andy White
+6  A: 

If everything 'regular expressions' related thing/book/tutorial has failed you, you might need to go deeper.

Learn automata theory, grab a book, do the exercises and relate your neat little DFAs to actual regular expressions. That might illuminate you.

Regular expressions as we know them are just applied automata theory.

Vinko Vrsalovic
Except that many "regular" expression syntaxes aren't regular and cannot be implemented using finite automata. http://swtch.com/~rsc/regexp/regexp1.html - but remember it's not just that they aren't usually implemented as finite automata, but also (e.g. when using backreferences) that they can't be.
Steve314
@Steve314: Correct. Still, grasping how FAs work is a great help towards grasping how REs work, even though most RE engines are in fact IE engines.
Vinko Vrsalovic
@Vinko - Internet Explorer engines? I don't think they're *that* bad!
Steve314
+1  A: 
  • When folks help you with your REs, ask them why they did what they did.
  • Or, ask the same questions here. You'll get other opinions and maybe someone else's explanation will click.
John at CashCommons
+1  A: 

There's loads of good books and articles online.

Some of the books I've used are

But to be honest I learn the most through trial and error and on this process I found tools like Regulazy Roy Osherove very useful.

Some useful online resources that I've used are

Alan
A: 

i am much less years in programming, but let me tell my way into Regex. i first encountered them in university and couldn't undestand them. then during course of math logic we studied state machines and regex from math point of view. and it is really simple if you start from that point! try to search for articles about state machines and regular languages.

Andrey