DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Ruby Regular Expresssions

12.19.2008
| 3827 views |
  • submit to reddit
        These are some regular expressions that I use in Ruby, which I find useful from time to time.  Sadly some aren't as terse as they would be in Perl, due mostly to the fact that Ruby doesn't support look behind.

Firstly, adding commas to large integers:

>> string = '12345678'
=> '1234567'
>> regex = /(\d)(?=(\d\d\d)+$)/
=> /(\d)(?=(\d\d\d)+$)/
>> string.gsub(regex, '\1,')
=> '12,345,678'