Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
This week, we're updating our course layouts and descriptions. Presentation and materials always gently change over time, but just occasionally there's a need to make a step change to clear out some of the old and roll in the new. That's now happening - but over a long and complex site it's not instant and you'll see sections of the site changing up to and including 19th September.

See also [here] for status update
 
Ruby Regular Expressions

If you want to say does this string "look like" another, without being able to give an explicit string for the 'another', then you're probably looking for a regular expression.

REGULAR EXPRESSION ELEMENTS

Regular expressions come in various flavours, and the flavour used in Ruby is "Perl Style" (i.e. it's similar to Perl, Python and the preg functions in PHP, and differs from Tcl and the ereg functions in PHP).

In summary, a regular expression comprises elements such as:

Literals - exact matches
 - letters, digits and some special characters match exactly
 - special characters can be matched when preceded by a \

Character groups - any one character from a selection
 - [abcyh] any character from the list
 - [^abcyh] any character not in the list
 - [A-Z0-4] any capital letter of digit 0 through 4
 - \s \d \w any space, digit or word character
 - \S \D \W any non-space, non-digit or non-word character
 - . any character at all (may not match \n)

Counts - apply to previous literal, character or group
 - {2,6} 2 to 6 occurrences of previous item
 - {4,} 4 or more occurrences of previous item
 - {5} exactly 5 occurrences of previous item
 - + 1 or more of previous item
 - * 0 or more of previous item
 - ? 0 or 1 of previous item

Anchors (a.k.a. Zero width assertions)
 - ^ match here at start of string or line
 - $ match here at end of string or line
 - \b match here at word boundary

Miscellany
 - ( .... ) grouping for capture and counting
 - | alternation; "either / or"
 - \1 \2 references back to previous groups.

There are more options - but those are the common ones.

USE OF REGULAR EXPRESSIONS IN RUBY

The =~ operator is the 'match' operator, so I can ask if something looks like a regular expression. The index method on a string will recognise a regular expression and use it to separate. The variable $& contains the matched string, and $1, $2 etc contain matched capture groups.

Example:

places = ["Training in Melksham and elsewhere",
   "We are at SN12 6QL (HQ) and SN12 7NY (training centre)",
   "And can train you at even if you're at HS7 5LZ"]

# Matching to see whether or not it fits the pattern
places.each do |place|
        if place =~ /\b[A-Z]{1,2}\d\w?\s+\d[A-Z]{2}\b/
                puts %Q!There's a postcode in "#{place}"!
        end
end

# Making use of the matched string
places.each do |place|
        if place =~ /\b([A-Z]{1,2}\d\w?)\s+\d[A-Z]{2}\b/
                puts %Q!We found #{$&} sorted via #{$1}!
        end
end

# More careful extraction - global matching to regular
# expressions is not brilliant except in very recent
# releases, but the index method on string does very well
places.each do |place|
sf = 0
while sfn = place.index(/\b(([A-Z]{1,2}\d\w?)\s+\d[A-Z]{2})\b/,sf)
                sf = sfn+1
                puts %Q!We found #{$&}!
        end
end

Here is the result of running that program:

earth-wind-and-fire:~/ruby/r109 grahamellis$ ruby rex1.rb
There's a postcode in "We are at SN12 6QL (HQ) and SN12 7NY (tr ..."
There's a postcode in "And can train you at even if you're at HS7 5LZ"
We found SN12 6QL sorted via SN12
We found HS7 5LZ sorted via HS7
We found SN12 6QL
We found SN12 7NY
We found HS7 5LZ
earth-wind-and-fire:~/ruby/r109 grahamellis$

Ruby also allows you to produce compiled regular expression objects, and matchdata objects, and use those for more efficient and more sophisticated matching. And methods such as split, too, can use regular expressions.

DATA.read.each_line do |host|
        print host
        stuff = host.split(/[\s,]+/)
        ip = stuff.shift
        stuff.each do |name|
                print "#{ip} may be called #{name}\n"
        end
end

__END__
192.168.200.66 earth
192.168.200.67 fire, sea pickle
192.168.200.68 wind blows

Results:

earth-wind-and-fire:~/ruby/r109 grahamellis$ ruby rex2.rb
192.168.200.66 earth
192.168.200.66 may be called earth
192.168.200.67 fire, sea pickle
192.168.200.67 may be called fire
192.168.200.67 may be called sea
192.168.200.67 may be called pickle
192.168.200.68 wind blows
192.168.200.68 may be called wind
192.168.200.68 may be called blows
earth-wind-and-fire:~/ruby/r109 grahamellis$

Although we've been using / delimiters for regular expressions, you can if you prefer user %r! through ! (or replace the ! with any other special character) in much the same was as %Q and %q


See also Programming in Ruby - Course

Please note that articles in this section of our web site were current and correct to the best of our ability when published, but by the nature of our business may go out of date quite quickly. The quoting of a price, contract term or any other information in this area of our website is NOT an offer to supply now on those terms - please check back via our main web site

Related Material

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)
  [3621] Matching regular expressions, and substitutions, in Ruby - (2012-02-23)
  [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)

Ruby - Introduction
  [2607] Answers on Ruby on Rails - (2010-01-30)
  [2287] Learning to program in Ruby - examples of the programming basics - (2009-07-15)
  [2286] New to programming? It is natural (but needless) for you to be nervous - (2009-07-14)
  [1887] Ruby Programming Course - Saturday and Sunday - (2008-11-16)
  [1375] Python v Ruby - (2007-10-02)
  [1302] Ruby, Ruby, Ruby. Rails, Rails, Rails. - (2007-08-13)
  [1041] Learnt in London - Ruby, Martini, Coral and the Core - (2007-01-17)
  [1027] Cue the music, I'm happy. - (2007-01-09)
  [664] Ruby course - oops - it's not happening - (2006-04-01)
  [550] 2006 - Making business a pleasure - (2006-01-01)

resource index - Ruby
Solutions centre home page

You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum.

At Well House Consultants, we provide training courses on subjects such as Ruby, Lua, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site.

You can Add a comment or ranking to this page

© 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/solutions/ruby-rub ... sions.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard