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!

About Ashiq Alibhai

Ashiq Alibhai, PMP, has been a Rails aficionado since 2007, and developed web applications since early 2003, where he learned PHP in one summer. As the driving-force behind RailsRocket and the Launchpad project, he seeks to share the ease of development with Rails far and wide.
This entry was posted in Development and tagged , . Bookmark the permalink.

18 Responses to Regular Expressions in Ruby (and Rails)

  1. Tony says:

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

  2. Pingback: Essential Guide To Regular Expressions: Tools and Tutorials | Developer's Toolbox | Smashing Magazine

  3. Pingback: Essential Guide To Regular Expressions: Tools and Tutorials | Developer's Toolbox | Smashing Magazine

  4. Pingback: Essential Guide To Regular Expressions: Tools and Tutorials | Developer's Toolbox | Smashing Magazine

  5. Pingback: Essential Guide To Regular Expressions: Tools and Tutorials | Developer's Toolbox | Smashing Magazine

  6. Pingback: Essential Guide To Regular Expressions: Tools and Tutorials | Developer's Toolbox | Smashing Magazine

  7. Pingback: Essential Guide To Regular Expressions: Tools and Tutorials | Developer's Toolbox | Smashing Magazine

  8. Pingback: Essential Guide To Regular Expressions: Tools and Tutorials | Developer's Toolbox | Smashing Magazine

  9. Pingback: Essential Guide To Regular Expressions: Tools and Tutorials

  10. Pingback: Essential Guide To Regular Expressions: Tools and Tutorials | Programming Blog

  11. Pingback: Essential Guide To Regular Expressions: Tools and Tutorials | ClickLogin Web Design

  12. Pingback: Essential Guide To Regular Expressions: Tools and Tutorials | ClickLogin Web Design

  13. Pingback: Essential Guide To Regular Expressions: Tools and Tutorials | WORDPRESS-TEMPLATES-PLUGINS-RSS | Essential Guide To Regular Expressions: Tools and Tutorials >>>

  14. Pingback: Wordpress Blog Services - Essential Guide To Regular Expressions: Tools and Tutorials

  15. Pingback: Essential Guide To Regular Expressions: Tools and Tutorials | Misr IT Reader

  16. Pingback: Essential Guide To Regular Expressions: Tools and Tutorials | -- KiaraNews --

  17. Pingback: Essential Guide To Regular Expressions: Tools and Tutorials | rapid-DEV.net

  18. Pingback: AMB Album » Essential Guide To Regular Expressions: Tools and Tutorials

Comments are closed.