July 2011
Intermediate to advanced
400 pages
8h 46m
English
You can also use a regular expression to match one or more substrings. To do this, you should put part of the regular expression between parentheses. Here I have two groups (sometimes called captures): The first tries to match the string “hi”, and the second tries to match a string starting with “h” followed by any three characters (a dot means “match any single character,” so the three dots here will match any three consecutive characters) and ending with “o”:
groups.rb
/(hi).*(h...o)/ =˜ "The word 'hi' is short for 'hello'."
After evaluating groups in a regular expression, a number of variables, equal to the number of groups, will be assigned the matched value of those groups. These variables take the form of a $ followed by a number: ...
Read now
Unlock full access