Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Ruby Regular Expresssions

DZone's Guide to

Ruby Regular Expresssions

·
Free Resource
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'
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}