Training, Open Source Computer Languages

This is page http://www.wellho.net/mouth/3621_Mat ... -Ruby.html

Our email: info@wellho.net • Phone: 01144 1225 708225

Matching regular expressions, and substitutions, in Ruby

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
  [3758] Ruby - standard operators are overloaded. Perl - they are not - (2012-06-09)
  [3757] Ruby - a teaching example showing many of the language features in short but useful program - (2012-06-09)
  [3424] Divide 10000 by 17. Do you get 588.235294117647, 588.24 or 588? - Ruby and PHP - (2011-09-08)
  [2980] Ruby - examples of regular expressions, inheritance and polymorphism - (2010-10-02)
  [2623] Object Oriented Ruby - new examples - (2010-02-03)
  [2621] Ruby collections and strings - some new examples - (2010-02-03)
  [2614] Neatly formatting results into a table - (2010-02-01)
  [2608] Search and replace in Ruby - Ruby Regular Expressions - (2010-01-31)
  [2295] The dog is not in trouble - (2009-07-17)
  [2293] Regular Expressions in Ruby - (2009-07-16)
  [1891] Ruby to access web services - (2008-11-16)
  [1887] Ruby Programming Course - Saturday and Sunday - (2008-11-16)
  [1875] What are exceptions - Python based answer - (2008-11-08)
  [1588] String interpretation in Ruby - (2008-03-21)
  [1305] Regular expressions made easy - building from components - (2007-08-16)
  [1195] Regular Express Primer - (2007-05-20)
  [987] Ruby v Perl - interpollating variables - (2006-12-15)
  [986] puts - opposite of chomp in Ruby - (2006-12-15)
  [970] String duplication - x in Perl, * in Python and Ruby - (2006-12-07)


Back to
Finding the total, average, minimum and maximum in a program
Previous and next
or
Horse's mouth home
Forward to
Loading Ruby classes - where does Ruby look?
Some other Articles
An update on Billy the Greyhound and Gypsy the Lurcher
Why do we need a Model, View, Controller architecture?
Some TestWise examples - helping use Ruby code to check your web site operation
Loading Ruby classes - where does Ruby look?
Matching regular expressions, and substitutions, in Ruby
Finding the total, average, minimum and maximum in a program
Ruby v Perl - a comparison example
lists and struct::list in Tcl - Introduction to struct::list and examples
The fileutil package and a list of file system commands in Tcl
Bus top - colours of London
4280 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3621_Mat ... -Ruby.html • PAGE BUILT: Sun Mar 30 15:20:58 2014 • BUILD SYSTEM: WomanWithCat