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: ,     Posted in Development

Rate this article:
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...

Related Content


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. Essential Guide To Regular Expressions: Tools and Tutorials | Developer's Toolbox | Smashing Magazine Says:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    [...] Regular Expressio&#110&#115&#32in Ruby and RailsRegular expressions in Rails are &#98&#114&#97cketed by forward-slash, so a regular expression l&#111&#111&#107s like this: /[0-9]*/. You can put all your usual &#109&#111&#100ifiers after the second slash (such as i for case-&#105&#110&#115ensitivity). Gone are other programming languages

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

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

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

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

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

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

Leave a Reply