Ruby/Regular expressions and blocks

From Meshplex

Jump to: navigation, search
Image:Ruby_on_rails_tutorials.jpg
Ruby for complete beginners
Ruby Introduction
What can I use RoR for?
Reasons for choosing RoR over other popular programming languages such as php or asp.net .What makes Ruby so much more special
Where can I find RoR? In what “forms” does it come?
How to install RoR.Solutions for both the novice and professional programmers on Windows,Mac OS X and Linux.Prerequisites.
Ruby programming tutorials for beginners:
Ruby Basics
Ruby Variables, Datatypes, Operators
Ruby Symbols
Ruby Statements
Ruby Converting data to another type: type conversion or typecasting
Ruby Arrays, Hashes, Ranges
Ruby Functions and built in functions
Ruby Control structures
Ruby Regular expressions and blocks
** Ruby Blocks
Ruby Loops
Ruby Recursion
Ruby Data Structures
Ruby Methods, Classes, Modules, Namespaces
Ruby Exceptions
Ruby Object Oriented Programming
Ruby Multithreading
Ruby File Handling.Input and Output
Ruby Basic GUI
Ruby and databases.Ruby on Rails and MySQL
Ruby Basic CGI.Using fastCGI
Ruby Basic Networking and web programming
Ruby Basic Graphics
Ajax and Rails.Web 2.0 and what it means
Ruby Testing, Debugging, Automation of tasks
Ruby Apache,Capistrano, Mongrel,lighttpd – reviews and tips
Finding a Ruby on Rails ready web hosting company
BONUS: mini tutorial for a simple RoR application

[edit] Ruby Regular expressions and blocks

] range specificication (e.g., [a-z] means a letter in the range a to z)
\w letter or digit; same as [0-9A-Za-z]
\W neither letter or digit
\s space character; same as [ \t\n\r\f]
\S non-space character
\d digit character; same as [0-9]
\D non-digit character
\b backspace (0x08) (only if in a range specification)
\b word boundary (if not in a range specification)
\B non-word boundary
* zero or more repetitions of the preceding
+ one or more repetitions of the preceding
{m,n} at least m and at most n repetitions of the preceding
 ? at most one repetition of the preceding; same as {0,1}
| either preceding or next expression may match
() grouping

If you are familiar with the Perl or Python programming languages or at least have seen Perl syntax, Ruby regular expressions won't be too much of a shock for you.If, however, you are not familiar with any other programming languages, then regular expressions might be a bit strange at first, but once you understand what they're used for and why syntax had to be as uncomplicated as possible you might start to actually like them.

The main reason regular expressions exist is this: if you want your program to check if "something" looks like something else, in terms of similar characters or spacing or lenght or many many other things you might think of, you use a regular expression. The table above will give you a concise view of all Ruby's regular expression elements.

Regular expressions are used for matching certain patterns, so for example, if you use \d then this will try to match a digit, if you use \s, this will match a space character.

Let me now give you an example on how you use regular expressions:

johnsays = “It's 9:18PM here now.I cannot wait to go out tonight.”
time = /\d\d:\d\d/
if johnsays=~time
puts “John told you what time it is.He is eager to go out.”
else
puts “John does not care what time it is.He is too tired to go out.”
end

The =~ is the match operator.

Basically, what this code does is this:

In the variable johnsays you store a string, which in this case contains a string of chars indicating an hour in standard format also.The variable time stores the pattern to search for: in this case, 2 digits, followed by a semicolon, followed by another 2 digits.Notice you you need to put a forward slash / before the first \d and how you also need to a add a backwards slash after the last \d You need to do this each time you want to place the patterns to search for inside a variable.

There is another way of using regular expressions.Since Ruby is an object-oriented programming language, the "johnsays" expression becomes a member of the Regexp class, which is the regular expressions class in Ruby.

Therefore, you could also write the expression as

johnsays = Regexp.new(‘\d\d:\d\d’)

using the .new method for the Regexp class.

Whenever a match is found, a number of variables are created by Ruby.

Here are some examples with added details:

puts “Characters before the match : #{$`}”
 
puts “Matched characters : #{$&}”
 
or
 
puts “Matched characters : #{$~}”
 
puts “Characters after the match : #{$’}”
 
puts “Tird part of the match : #{$3}”

Try these yourself and notice the outputs.

Previous Next