String#split and regex lookarounds

September 11, 2007 Pivotal Labs

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”

this will do nicely:

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…

About the Author

Biography

Previous
Treetop 1.0.1
Treetop 1.0.1

I've been working on a parsing framework for Ruby since January called Treetop. I just released version 1.0...

Next
Ruby quiz du jour answer
Ruby quiz du jour answer

Josh got it right. 1.0/0 indeed resolves to "Infinity". Pretty cool! -Felix

×

Subscribe to our Newsletter

!
Thank you!
Error - something went wrong!