For 2023 (and 2024 ...) - we are now fully retired from IT training. We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.
Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!
I am also active in many other area and still look after a lot of web sites - you can find an index ((here)) |
Ruby module R109
Strings and Regular Expressions
Exercises, examples and other material relating to training module R109. This topic is presented on public courses Learning to program in Ruby, Ruby Programming
Background Strings and regular expressions are the core of
most Ruby applications and you'll be thrilled at the
broad scope of methods available to you. Related technical and longer articles Pattern Matching - a primer on regular ExpressionsRuby Regular ExpressionsString Functions in Ruby
Articles and tips on this subject | updated | 4549 | Clarrissa-Marybelle - too long to really fit? Would you christen your child Clarissa-MaryBelle? No, neither would I, but someone might - and there's a name that's so long it won't fit into boxes on forms.
When programming, there's a need to format things to line up, and in a ixed with font / plain text data file, that's done withspae pading - ... | 2015-10-23 | 4505 | 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 ... | 2015-06-03 | 4388 | Global Regular Expression matching in Ruby (using scan) Regular expression 'engines' start at the left of an input string they're matching against, and counts are usually greedy, so by default they return "leftmost, longest" matches - which works for what users want in most cases.
However, sometimes you may want to work through all (non-overlapping) matches ... | 2015-01-08 | 2980 | Ruby - examples of regular expressions, inheritance and polymorphism With delegates on public Well House Consultants courses staying in our on-site accommodation, there's time for them to learn a lot more about each other's applications and get a far wider view of the uses of the subject they're with us to learn. There's time for the delegate who wants to extend practicals ... | 2013-01-01 | 3758 | Ruby - standard operators are overloaded. Perl - they are not Ruby has been described to me as "What Perl 5.5 should have been", but that statement is a severe dis-service to Ruby, and to Perl. Ruby is a new language, and if you're moving into Ruby from Perl, you'll do best not to assume even the basics from Perl. A lot has been learend from Perl, for sure, ... | 2012-06-16 | 3757 | Ruby - a teaching example showing many of the language features in short but useful program Although the main publicity and driver for the Ruby language has been the Rails web framework (see previous article here), it's an excellent data manipulation language too - with many of the short and efficient coding techniques that you would have available to you in Perl, yet additionally with an object ... | 2012-06-16 (longest) | 3621 | 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 ... | 2012-02-27 | 3424 | Divide 10000 by 17. Do you get 588.235294117647, 588.24 or 588? - Ruby and PHP If you divide £10,000 between 17 lucky children, each gets £588.23 ... but if you divide the number 10,000 by the number 17, you get 588.235294117647. How do you format the data to the appropriate number of decimal places?
In Ruby, you can use the % operator on a string - which is an alias ... | 2011-09-08 | 2623 | Object Oriented Ruby - new examples We had an "object oriented Ruby" morning on today's course. "But everything's an object in Ruby" you'll tell me and you're sort of right (we could argue about what symbols and code blocks are), but there are certain elements of object orientation which require specific coverage.
Here are links to three ... | 2010-02-04 | 2621 | Ruby collections and strings - some new examples From yesterday's Ruby course - a whole set of new examples on Ruby collections - arrays (which are ordered lists) and hashes (which are unordered). In other words, you look somethng up in an array by its position number, whereas you look something up in a hash based on its unique key, and the way it's ... | 2010-02-03 | 2614 | Neatly formatting results into a table Would you like to produce a neat table of results, with the columns just wide enough to take the data? That sounds straightforward, but you can't output anything until you've looked through all the lines to see how long each of them will be.
I was talking about this algorithm today, and this evening ... | 2010-02-02 | 2608 | Search and replace in Ruby - Ruby Regular Expressions "If you want to replace one part of a string by another in Ruby, you can use the sub method on your string object. The first parameter you give to the method is the string you want to replace, and the second is the string you want to replace it by."
OK - that's the easy bit ... but what if ....
If ... | 2010-02-01 | 2295 | The dog is not in trouble "I know I put my papers somewhere" I said to Lisa [wife], and Gypsy [dog] goes off and whimpers in the corner, looking very guilty.
So had she [dog] taken the papers and chewed them? That wasn't the case - she had heard the word "no" (or rather "know") in what I said, and had taken it that she was ... | 2009-07-18 | 2293 | Regular Expressions in Ruby Ruby has a wide variety of string handling methods, and a very strong regular expression engine. In one of the example I wrote today ([link] Regular Expression Example in Ruby) I've included a primer on the elements of a Ruby Regular expression as well as sample source code - showing you how you can ... | 2009-07-16 | 1887 | Ruby Programming Course - Saturday and Sunday We're so busy during the week at the moment that I'm running an extra Ruby Course this weekend. The main use of Ruby is within "Ruby on Rails", but our course concentrates on the language itself and so it's suitable for people who are using RSpec, cucumber and Watir as well as Rails.
Here are some ... | 2008-11-16 | 1891 | Ruby to access web services If you want to use a Ruby program to access an RSS feed (or some other XML or HTML data), you can start with the standard Net::HTTP module ... full (working, tested) example here. That example "just" uses a Get method to get an RSS feed of the latest posts to the First Great Western Coffee Shop Forum ... | 2008-11-16 | 1875 | What are exceptions - Python based answer How do you check for run time errors in your program? You'll include tests with if statements in almost every program you write, to ensure that user entered data is reasonable / as you would expect ... but you will also need to do more that that. You'll need to check whether a system function has run ... | 2008-11-08 | 1588 | String interpretation in Ruby Ruby's rather clever in terms or how it handles string interpretation. Since variable names only start with special characters sometimes, it can't use Perl's trick of making the $ and @ character special in a string. So instead it uses a #{.....} notation. For example:
puts "Tocker is #{tocker}"
uses ... | 2008-03-22 | 1305 | Regular expressions made easy - building from components There seems to be a certain macho desire in many programmer's minds to write a single complicated regular expression to match against an input line, ignorning the structured approach that everyone accepts quite cheerfully in almost every other case. Have a look at this Python line:
wholeline = r"\d\d-...-\d\d\d\d\s+(\d\d):(\d\d):(\d\d.\d\d),\s+(-?\d+\.\d+),\s+(-?\d+\.\d+),(-?\d+\.\d+),\s+(-?\d+\.\d+),(-?\d+\.\d+),\s+(-?\d+\.\d+)"
Impressive, ... | 2007-08-16 | 1195 | Regular Express Primer Over the years I've been teaching people about Regular Expressions, I've learnt what does and doesn't work in such tuition. A casual question I saw yesterday got me writing, and I've just posted up a new technical article to the solution centre - see here (it will open in a separate window).
It's ... | 2007-05-21 | 987 | Ruby v Perl - interpollating variables When printing in Perl, you can drop a variable into a double quoted string ant it will be interpretter for you:
"--- $number ---"
If you want to perform an operation on the variable, though, it's not as easy - you have to come out of the double quoted string, do the calculation, then start another ... | 2006-12-21 | 986 | puts - opposite of chomp in Ruby In Ruby, the chomp method removes the last character of a string if it's a line separator. The puts method adds a new line character on to the output unless there's one already present.
In Perl and other languages, a great deal of time and mental agility is expended in remembering where there are (and ... | 2006-12-15 (short) | 970 | String duplication - x in Perl, * in Python and Ruby In Python and Ruby you can duplicate a string by running the multiply operator (*) on a string object, and in perl you can use the x operator. There have been many times when I've looked at this facility and said to myself "very nice, but isn't that a feature looking for a benefit" - in other words, ... | 2006-12-09 (short) |
Examples from our training material
bc | Some String Handling | d4_1 | Using a string as a collection | d4_6 | Formatted printing | d4_7 | Running a piped command via backquotes | d4_8 | Regular Expression (including documenation) | e2 | Embedded variables in strings | finder | search, search / replace, search / execute / replace | globalmatch | Scan for global matches | heredocs.rb | Here Documents in Ruby | ii | Formatting example - splitting money between nieces and nephews | kiddos | Formatting a table of data | kids | Formatting values in Ruby | mlre.rb | Commenting a regular expression | names | formatting - adjust for longest string | pareea | Class for email addresses / regex for splitting and checking | pc | extracting sections from a match | regex_sub.rb | Regular Expression Substitution | rex1.rb | Ruby regular expression demo | rex2.rb | Splitting at a regular expression | se1 | Regular Expression Primer Example | shalt | the compact method removes nil elements from an array | sop.rb | + and * on strings in Ruby | stdo.rb | Using some of ruby's string methods | str_types.rb | Ways of writing a string | summatelse | Regular Expression Alternative Example | vest | Cleaning up a phone number and checking a postcode |
Background information
Some modules are available for download as a sample of our material or under an Open Training Notes License for free download from [here].
Topics covered in this module
Anchors, literals, character groups and counts. Matching in Ruby. Modifiers i, o, x and m. Pattern matching variables.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule page.
Well House Consultants specialise in training courses in
Ruby,
Lua,
Python,
Perl,
PHP, and
MySQL. We run
Private Courses throughout the UK (and beyond for longer courses), and
Public Courses at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us.
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre.
|