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

Rails Regex Lookaround

DZone's Guide to

Rails Regex Lookaround

·
Free Resource
(Snagged from http://www.pivotalblabs.com/articles/2007/09/11/string-split-and-regex-lookarounds)

Ruby supports regular expression quite well, so it's not surprising that one can use a lookaround to match a specific position. That also works really well with String#split, so if one needs to extract the key/value out of a string such as this:


s = "TYPE=Exterior RED=119 GREEN=105 BLUE=88 TABLEREQ=PNTTBL-01 TABLE=Primary w/XL GENERICCLR=Beige GENERICCLRCODE=31"
s.split(/\s(?=[A-Z]*=)/)


Just splitting on "space" won't work because there are spaces inside the values, too ("Primary w/XML"). Simple and useful...
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 }}