A 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
$1 - 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" % $1.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].
(written 2012-02-23, updated 2012-02-27)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
R109 - Ruby - Strings and Regular Expressions [970] String duplication - x in Perl, * in Python and Ruby - (2006-12-07)
[986] puts - opposite of chomp in Ruby - (2006-12-15)
[987] Ruby v Perl - interpollating variables - (2006-12-15)
[1195] Regular Express Primer - (2007-05-20)
[1305] Regular expressions made easy - building from components - (2007-08-16)
[1588] String interpretation in Ruby - (2008-03-21)
[1875] What are exceptions - Python based answer - (2008-11-08)
[1887] Ruby Programming Course - Saturday and Sunday - (2008-11-16)
[1891] Ruby to access web services - (2008-11-16)
[2293] Regular Expressions in Ruby - (2009-07-16)
[2295] The dog is not in trouble - (2009-07-17)
[2608] Search and replace in Ruby - Ruby Regular Expressions - (2010-01-31)
[2614] Neatly formatting results into a table - (2010-02-01)
[2621] Ruby collections and strings - some new examples - (2010-02-03)
[2623] Object Oriented Ruby - new examples - (2010-02-03)
[2980] Ruby - examples of regular expressions, inheritance and polymorphism - (2010-10-02)
[3424] Divide 10000 by 17. Do you get 588.235294117647, 588.24 or 588? - Ruby and PHP - (2011-09-08)
[3757] Ruby - a teaching example showing many of the language features in short but useful program - (2012-06-09)
[3758] Ruby - standard operators are overloaded. Perl - they are not - (2012-06-09)
[4388] Global Regular Expression matching in Ruby (using scan) - (2015-01-08)
[4505] Regular Expressions for the petrified - in Ruby - (2015-06-03)
[4549] Clarrissa-Marybelle - too long to really fit? - (2015-10-23)
Some other Articles
An update on Billy the Greyhound and Gypsy the LurcherWhy do we need a Model, View, Controller architecture?Some TestWise examples - helping use Ruby code to check your web site operationLoading Ruby classes - where does Ruby look?Matching regular expressions, and substitutions, in RubyFinding the total, average, minimum and maximum in a programRuby v Perl - a comparison examplelists and struct::list in Tcl - Introduction to struct::list and examplesThe fileutil package and a list of file system commands in TclBus top - colours of London