I haven’t been fan to the regular expresion myself, but they are very useful and I try to learn each day to use their potencial.
I going to write down here some tips that I have found useful and I like to remember. I hope this helps anyone else.
First it’s important to remember that the
Strings are very powerfull per se and sometimes there is no need for a regular expresions. For example:
There are a lot of interesting methods for
Strings and I love them, but let’s see some cases for regex:
The tipical base case is the previous one, check if there is some text or pattern inside one string. This can be done by doing:
This will return the position in the string where the pattern start and
nil if the pattern doesn’t match. You can use it in a
if stament to see if this match. The positive value that indicate the position will count as
true for the
if, so we can follow with our workflow.
The string that match with the pattern will be stored in a special var. From
$9 are reserved for this purpose. But, nine vars? Yes, because we can search for more than one pattern and store their values in different variables. Those patterns are called groups and are delimited by parentesis. Lets see it:
1 2 3 4 5 6 7
Oviously is is very simple and silly example. Well this is very basic, but what I wanted to talk about is how to work better with those groups and special variables. Groups are used for capturing text, but also to define alternatives in a patterns. For example:
1 2 3 4
So the pattern will match eigther with
/\w+\.jpg/. This is very useful and is tipical from any language or tool that support regex, but is good to know.
But one interesting thing that I have learnt recently is how to avoid to feed the special variables with the matches’ results.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
But there is a nicer way to save the matches, we can store them in our own variables:
1 2 3 4 5 6 7 8
And you also can ignore any group. I found this useful at the
step_definitions for Cucumber. Here is one example I did time ago:
1 2 3 4
Cucumber checks for this regex againts the features and pass the groups founded to the block (as
arg2). In this perticular case I want a expresion that matched with that kind of sentence but I didn’t want to store
to. Mostly beacuse Cucumber highlight the groups matched at the output and I didn’t want for those words. I just wanted reuse the regex to both cases.
Anyways, I found
(?:regex) interesting and useful.
Well, I don’t know if someone find this useful, but at least I got this written down as alternative to my bad memory :-P
Oh, btw, If you are going to use regular expresions in Ruby I higly recomended you Rubular. Which is an awesome online Ruby regular expresion editor. Has some nice quick reference and check on-the-fly all your expresions and cases. Really cool :-)