Matching regular expressions, and substitutions, in Ruby
Archive - Originally posted on "The Horse's Mouth" - 2012-02-23 22:50:20 - Graham EllisA stranger comes up to me as I'm walking the dogs and asks me "Do you know the way to the Town Centre?". "Yes" I answer, and walk on. I've answered his question, but he probably meant to ask me to give him directions to the town center and my answer isn't really useful to him - it's not enough.
It's the same thing with Regular Expression matches - you can use a regular expression to say "does this piece of text contain a piece URL that's a Google query request"? ... but an answer of simply "yes" or "no" may be inadequate for you. You may want to know what the search terms entered by the user were, and perhaps which country server he used. And you may want to look elsewhere in the text to find out which page the search pointed to on your site.
Here (in Ruby) is a regular expression that looks for a Google referer in the variable called referer:
if referer =~ /\.google\..*[?&]q=[^&"]*/
and - being written within an if it will perform the following code only if there was a match. But what was the search string? I can modify my match ever so slightly:
if referer =~ /\.google\..*[?&]q=([^&"]*)/
and that will now capture the part of the incoming string that matches
[^&"]*
into the special global variable - which we can then make use of.So - not only have we said "yes, that's a match", but we've also said "here is the search terms from within the match", simply be identifying the bits we're interested in using round brackets.
But that may not be the whole story - indeed, in this case it isn't. I now want to ask "does the search term string contain any + characters? If so - I want to replace them with spaces. And "does the search term contain any 3 character sequences starting with %? If so - I want to replace the 3 characters with a single charcater, taking character no. 2 and character no.3 of the matched sequences as the hex code for the character.
Replacing + characters by spaces can be done by the tr! method on a string:
query.tr! '+', ' '
but handling the hex codes is a little more complex:
query = query.gsub(/%(..)/) { "%c" % .to_i(16) }
That's a regular expression global substitution, and because I have given a block rather than a replacement string, the replacement is run and the result of running it is substitued. Neat, short, but not entirely clear to the newcomer, and not easy to extract from the Ruby documentation - certainly a topic to cover on Ruby Courses, and something to write about here as well! Complete source of example [here].