| ] |
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.
|