Training, Open Source Programming Languages

This is page http://www.wellho.net/mouth/4505_Reg ... -Ruby.html

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

 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Regular Expressions for the petrified - in Ruby

Regular Expressions ... frighten ... newcomers at their apparent peverseness and complexity. But they need not - regular expressions are made up of just a handful of types of elements and once you realise this, they become easy!

The background is that you want to ask is a string of text looks like a particular pattern.

You describe the pattern, from the left, by specifiying a series of:
• specific characters that must be matched or
• character groups (where any one character from a list must be matched
and each of these specific characters or groups is follows by
• a count of the number of times that character of group is to be matched.

For example:
  [A-Z]{1,2}[0-9]{1}
means "1 or 2 letters between A and Z followed by one digit between 0 and 9"

Here's a full example to match a British Postcode:
  [A-Z]{1,2}[0-9]{1}[0-9A-Z]{0,1} {1,}[0-9]{1}[A-Z]{2}
So that's
• one or two letters
• a digit
• possibly another digit or letter
• some spaces
• a digit
• two letters

Alas ...

• that's getting longwinded and there are shortenings that make it more compact, but more complex-looking.
[0-9] can become \d
{1,} can become +
{1} can be left out as it's the default
{0,1} can become ?
and so on.

• having matched, there's usually a requirement in subsequent program lines to make use of the string that was matched, or the parts of it that matched - and there needs to be a mechanism (round brackets used) to indicate groups to captuure

• the regular expression needs some sort of wrapping within the language to indicate that it is to be treated as a pattern for matching rather than in other ways.

As a first illustration of regular expressions to match a postcode in Ruby, there's an example [here] from last week's course. In that example, I've only applied minimal optimisation to keep it clean. A further example [here] makes use of the shortenings above, and make use of alternative delimiters and ignore-case flags too.
(written 2015-06-03)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q802 - Object Orientation and General technical topics - Regular Expression Elements
  [453] Commenting Perl regular expressions - (2005-09-30)
  [1480] Next course - 7th January 2008, Regular Expressions - (2007-12-21)
  [1766] Diagrams to show you how - Tomcat, Java, PHP - (2008-08-22)
  [1799] Regular Expressions in PHP - (2008-09-16)
  [1849] String matching in Perl with Regular Expressions - (2008-10-20)
  [2804] Regular Expression Myths - (2010-06-13)
  [4763] Regex Reference sheet - (2017-10-10)

Q801 - Object Orientation and General technical topics - What are Regular Expressions?
  [1195] Regular Express Primer - (2007-05-20)
  [2563] Efficient debugging of regular expressions - (2010-01-04)
  [2844] Learning about Regular Expressions in C through examples - (2010-06-30)

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)
  [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)
  [3621] Matching regular expressions, and substitutions, in Ruby - (2012-02-23)
  [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)
  [4549] Clarrissa-Marybelle - too long to really fit? - (2015-10-23)


Back to
Where does Ruby load modules from, and how to load from current directory
Previous and next
or
Horse's mouth home
Forward to
Peak weekend - where there are still rooms near Melksham
Some other Articles
Throwing a die - exercise in Ruby
Melksham to London by train - dont buy more than you need
In favour of adoption rather than puppy purchase - dogs!
Peak weekend - where there are still rooms near Melksham
Regular Expressions for the petrified - in Ruby
Where does Ruby load modules from, and how to load from current directory
Separating your code for easier testing, understanding and re-use; example in Ruby
Reading and parsing a JSON object in Ruby
Defining the behaviour of your web site and testing that it works
The TransWilts Community Intergrated Transport Corridor
4759 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, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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., 2021: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4505_Reg ... -Ruby.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb