Regular Expressions in Ruby (and Rails)
If you’re new to Regular Expressions, well, go read regular-expressions.info, then come back.
If you’re wondering how Rails (and, consequentially, Ruby) deals with Regular Expressions, you’ve come to the right place.
Syntax
Regular expressions in Rails are bracketed by forward-slash, so a regular expression looks like this: /[0-9]*/. You can put all your usual modifiers after the second slash (such as i for case-insensitivity). Gone are other programming languages’ ways of dealing with regular expressions as a string!
Matching Expressions
As you’d expect, you match expressions against string. Hence the String class has a match function that takes a regular expression, and returns the partially-matched string.
"Hello".match(/l/) => "l"
Great stuff! You can do all your usual complex matching. And if nothing is matched, you get nil:
"Hello".match(/x/) => nil
This allows you to easily write validation code (in Ruby, since Rails has its own Validation framework) like so:
if credit_card.match(/[0-9]{16}/).nil?
puts "Please enter a valid 16-digit credit-card number!"
else
# process order
end
Replacing Matched Groups
As mentioned above, you can check for matches by checking if match returns nil. But what if you want to replace a matched piece of text with something else?
For example, with credit-card storage, it’s a common thing that you want to cross out the first 12 digits, and only print the last four. In Regular Expression parlance, these groups are called “backreferences,” because they refer back to what you match.
Check this example out, where we have four groups:
VALID_CREDIT_CARD = /([0-9]{4})-([0-9]{4})-([0-9]{4})-([0-9]{4})/
If you wanted to access the first group of four digits, you write \1 (or, in a string, \\1, to back-quote the slash). The second set is \2, and so on.
How can you use this? Say you want to replace the middle two groups with “XXXX”; you can use string.gsub, which takes a regular expression, and performs a replacement, like so:
"1234-5678-9012-3456".gsub(VALID_CREDIT_CARD, "\\1-XXXX-XXXX-\\2") => 1234-XXXX-XXXX-3456
Needless to say, the possibilities are endless. You can create almost anything you can imagine with this simple tool–everything from credit-card hiding to a notation language like RedCloth to an HTML cleanup tool. Stretch your imagination!
And in fact, this is the core functionality in WebWrench, our Ruby static website generation tool. Check it out, and see what you learn.
And that’s regular expressions … the Ruby/Rails way!
Tags: regular expressions, string replacement Posted in


April 27th, 2009 at 11:34 am
“1234-5678-9012-3456″.gsub(VALID_CREDIT_CARD, “\\1-XXXX-XXXX-\\2″) => 1234-XXXX-XXXX-3456
Wouldn’t that be \\1-XXXX-XXXX-\\4 ?
I’d probably drop groups 2 and 3 from the VALID_CREDIT_CARD regexp however – it is safer.
June 1st, 2009 at 5:21 pm
[...] Regular Expressions in Ruby and RailsRegular expressions in Rails are bracketed by forward-slash, so a regular expression looks like this: /[0-9]*/. You can put all your usual modifiers after the second slash (such as i for case-insensitivity). Gone are other programming languages’ ways of dealing with regular expressions as a string! [...]
June 1st, 2009 at 5:21 pm
[...] Regular Expressions in Ruby and RailsRegular expressions in Rails are bracketed by forward-slash, so a regular expression looks like this: /[0-9]*/. You can put all your usual modifiers after the second slash (such as i for case-insensitivity). Gone are other programming languages’ ways of dealing with regular expressions as a string! [...]
June 1st, 2009 at 5:21 pm
[...] Regular Expressions in Ruby and RailsRegular expressions in Rails are bracketed by forward-slash, so a regular expression looks like this: /[0-9]*/. You can put all your usual modifiers after the second slash (such as i for case-insensitivity). Gone are other programming languages’ ways of dealing with regular expressions as a string! [...]
June 1st, 2009 at 5:21 pm
[...] Regular Expressions in Ruby and RailsRegular expressions in Rails are bracketed by forward-slash, so a regular expression looks like this: /[0-9]*/. You can put all your usual modifiers after the second slash (such as i for case-insensitivity). Gone are other programming languages’ ways of dealing with regular expressions as a string! [...]
June 1st, 2009 at 5:21 pm
[...] Regular Expressions in Ruby and RailsRegular expressions in Rails are bracketed by forward-slash, so a regular expression looks like this: /[0-9]*/. You can put all your usual modifiers after the second slash (such as i for case-insensitivity). Gone are other programming languages’ ways of dealing with regular expressions as a string! [...]
June 1st, 2009 at 5:21 pm
[...] Regular Expressions in Ruby and RailsRegular expressions in Rails are bracketed by forward-slash, so a regular expression looks like this: /[0-9]*/. You can put all your usual modifiers after the second slash (such as i for case-insensitivity). Gone are other programming languages’ ways of dealing with regular expressions as a string! [...]
June 1st, 2009 at 5:21 pm
[...] Regular Expressions in Ruby and RailsRegular expressions in Rails are bracketed by forward-slash, so a regular expression looks like this: /[0-9]*/. You can put all your usual modifiers after the second slash (such as i for case-insensitivity). Gone are other programming languages’ ways of dealing with regular expressions as a string! [...]
June 1st, 2009 at 5:55 pm
[...] Regular Expressions in Ruby and RailsRegular expressions in Rails are bracketed by forward-slash, so a regular expression looks like this: /[0-9]*/. You can put all your usual modifiers after the second slash (such as i for case-insensitivity). Gone are other programming languages’ ways of dealing with regular expressions as a string! [...]
June 1st, 2009 at 6:22 pm
[...] Regular Expressions in Ruby and RailsRegular expressions in Rails are bracketed by forward-slash, so a regular expression looks like this: /[0-9]*/. You can put all your usual modifiers after the second slash (such as i for case-insensitivity). Gone are other programming languages’ ways of dealing with regular expressions as a string! [...]
June 1st, 2009 at 6:23 pm
[...] Regular Expressions in Ruby as good as RailsRegular expressions in Rails have been bracketed by forward-slash, so a unchanging countenance looks similar to this: /[0-9]*/. You can put all your usual modifiers after a second condense (such as i for case-insensitivity). Gone have been alternative programming languages’ ways of traffic with unchanging expressions as a string! [...]
June 1st, 2009 at 6:27 pm
[...] Regular Expressions in Ruby as good as RailsRegular expressions in Rails have been bracketed by forward-slash, so the unchanging countenance looks similar to this: /[0-9]*/. You can put all your usual modifiers after the second condense (such as i for case-insensitivity). Gone have been alternative programming languages’ ways of traffic with unchanging expressions as the string! [...]
June 1st, 2009 at 6:40 pm
[...] Regular Expressions in Ruby and RailsRegular expressions in Rails are bracketed by forward-slash, so a regular expression looks like this: /[0-9]*/. You can put all your usual modifiers after the second slash (such as i for case-insensitivity). Gone are other programming languages’ ways of dealing with regular expressions as a string! [...]
June 1st, 2009 at 7:11 pm
[...] Regular Expressions in Ruby and RailsRegular expressions in Rails are bracketed by forward-slash, so a regular expression looks like this: /[0-9]*/. You can put all your usual modifiers after the second slash (such as i for case-insensitivity). Gone are other programming languages’ ways of dealing with regular expressions as a string! [...]
June 6th, 2009 at 12:07 am
[...] Regular Expressions in Ruby and RailsRegular expressions in Rails are bracketed by forward-slash, so a regular expression looks like this: /[0-9]*/. You can put all your usual modifiers after the second slash (such as i for case-insensitivity). Gone are other programming languages
June 6th, 2009 at 12:40 am
[...] Regular Expressions in Ruby and RailsRegular expressions in Rails are bracketed by forward-slash, so a regular expression looks like this: /[0-9]*/. You can put all your usual modifiers after the second slash (such as i for case-insensitivity). Gone are other programming languages’ ways of dealing with regular expressions as a string! [...]
June 14th, 2009 at 11:01 pm
[...] Regular Expressions in Ruby and RailsRegular expressions in Rails are bracketed by forward-slash, so a regular expression looks like this: /[0-9]*/. You can put all your usual modifiers after the second slash (such as i for case-insensitivity). Gone are other programming languages’ ways of dealing with regular expressions as a string! [...]
August 5th, 2009 at 4:14 am
[...] Regular Expressions in Ruby and RailsRegular expressions in Rails are bracketed by forward-slash, so a regular expression looks like this: /[0-9]*/. You can put all your usual modifiers after the second slash (such as i for case-insensitivity). Gone are other programming languages’ ways of dealing with regular expressions as a string! [...]