Close
Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

LABS
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″

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…

Comments
Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *