Thursday, October 3, 2013

My humble Regex beginnings

I understand what Regex's are used for and how powerful they can be. But I've yet to use them regularly enough to learn more than a simple chart lookup here and there as a cheat sheet when I need it. I'm not much farther than that tonight, and it's already late so I don't think I will be going much farther either. I would like to share a few things that I've found out, and if I share them here, maybe I'll have a better chance of remembering them...

To really learn regex, I've been watching videos which means I'm just learning the material from someone else and spitting it back to you guys in layman's terms. In fact, for a while, I would bet on the fact that it will probably be mostly in this format for a while. I'm still new(ish) so, hang in there and we'll start moving forward together. PS - I want to link newthinktank.com since it was Derek from there that gave me a decent start in regex tonight. His videos also helped me realize my statements below really ring true when learning regex.

So the first thing I really took away from tonight's video session with regex is that, to truly learn it, you should write out what you want to accomplish in notepad to make sure it all looks correct. While doing so, there is no pressure to get it 100% right the first time. Just start writing it! You can work it out as you go! Along with that, you can also put the regex in any format that helps you learn better. So a perfect way Derek showed me was to take it and put each portion of regex on a newline. 

Example:
\d{3}       The d here represents searching for a 'digit' that is 3 digits in length ONLY
\s            The s here represents searching for a 'space'
\w+         The w means character, the plus at the end means "of any length (one or more)"
PS - The capital of each letter is usually a term for the exact opposite of what you were searching for. So 'S' means "anything BUT a space".

The above code would look completely wonky and overwhelming when put in a line as it's seen in use. So if you were looking for a 3 digit number followed by a space then a word... The regular expression you might see could look like \d{3}\s\w+ and it looks 100% foreign unless you know regex. So, a great way to learn, is to put it in a notepad and make each command you are learning (each small bit of a section) be a newline. When doing so, you break it down into small chunks of learning and hopefully that will help grasp it better.

This may even be able to be applied to many other scripting, programming, and shell functions and commands that you need to learn. It's common knowledge that learning in small chunks often is MUCH better than large dumps.


-Mitch

No comments:

Post a Comment