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)
Some other Articles
Throwing a die - exercise in RubyMelksham to London by train - dont buy more than you needIn favour of adoption rather than puppy purchase - dogs!Peak weekend - where there are still rooms near MelkshamRegular Expressions for the petrified - in RubyWhere does Ruby load modules from, and how to load from current directorySeparating your code for easier testing, understanding and re-use; example in RubyReading and parsing a JSON object in RubyDefining the behaviour of your web site and testing that it worksThe TransWilts Community Intergrated Transport Corridor