How the heck do I tackle them?
Like any new thing:
10 Study
20 Practice
30 goto 10
Study
I find that most successful teachers start teaching any subject by first providing a bit of background to the subject. It's important to have a context of what you're learning and, most importantly, why you're learning it.
It's all string matching
Regular Expressions are a means of matching patterns in text. It's a declarative language itself incorporated into numerous other programming language.
I'd like to emphasize that it's a declarative language, regular expressions are useful for expressing what string to match, but they do not in any way express how the program is to go about doing the matching. For this reason it's possible to use regular expressions very quickly and very slowly in the same programming language simply by using a different RegEx parser.
The reason for creating regular expressions are the same for the creation of most programming languages: programmers found themselves performing the same complicated task over and over and decided they wanted a simpler way of writing the code.
Some will (and should) complain about my previous sentence by saying something along the lines of:
RegEx doesn't make a program simpler.
it's true
RegEx doesn't make a program any simpler, RegEx makes writing the program simpler. You still need to be thorough in your testing to be certain that all correct cases are matched correctly, and all incorrect cases are not. It's really hard to test "all", and with complicated patterns, it's really hard to test "most". At worst, you should still be testing "some" cases.
lets incorporate some examples I've obligatorily chosen JavaScript's RegEx engine because I can test it live in the browser easily and because I won't have to do any string escaping while using RegEx literals.
When you do normal string matching, you test one string value against another. They can come from anywhere, but in the end it takes two strings being compared against one-another:
if ( 'foo' == 'bar' ) doSomething();
That example sucks because it will never do anything
if ( foo == 'bar' ) doSomething();
Much better; now, we don't actually know ahead of time whether or not something will be done. We can now start accepting user input:
if ( prompt( 'Say "bar" to do something.' ) == 'bar' ) doSomething();
Wonderful, now users can input bar
and something will happen, until you get bug reports from users saying that "bar"
isn't working, or that "BAR" isn't working, or that they've typed BRA
100 times and nothing ever happens.
Ignoring the misspellings and extra characters, 'bar' != 'BAR'
, and programmers need to think up a way of testing for where characters are the wrong case.
Simple solution, use toLowerCase
. That works wonderfully, but what about our users who are using British English over American English when you're matching something == 'color'
? Now you'll have to match something == 'color' || somthing == 'colour'
.
Long story short, simple patterns turn into lots of repetitive code very quickly.
The color example can simply be matched with:
/colou?r/.test( something )
a solid understanding of the basics of regular expressions can significantly reduce the amount of time you waste reinventing the wheel.
Where to study
Most languages that implement regular expressions have at least one resource available for the specific syntax of using Regular expressions within that language. One for JavaScript can be found on MDN
read it.
all of it.
then read it again.
It takes time to learn, think of it as an investment: an hour to learn RegEx now saves an hour the next time you need to do some string pattern matching, and then another hour the next time after that.
Practice
After reading all about RegEx, you probably wont understand most of it. That's because you're not actually doing anything with it.
I mentioned why I chose JS for this example, I urge you to mess with it in your browser. It's quick, and you can do it right in your URL bar.
JS has a few different and simple ways of using RegEx:
string.match( regex )
regex.exec( string )
regex.test( string )
Starting with something simple like:
javascript:'color'.match(/colou?r/);
is an easy way to get your foot in the door. Play with it, break it see what matches, and what doesn't.
When you get stuck on practice, continue to 30
. You need to read to learn more, but you need to practice to truly understand what you've learned.