Parsing odd string with Ruby -
an api responds string such following.
"\"approved\"|\"222222\"|\"11111111\"|\"\"|\"m\"|\"\"|\"5454\"|\"mc\"" i using following code parse
str = str.scan(/\w+/) this worked fine, str[0], str[1], etc..
than response such as
"\"declined\"|\"\"|\"64243178\"|\"\"|\"\"|\"\"|\"invalid exp date\"|\"\"|\"5454\"|\"mc\"" trying parse invalid exp date ends simply
str[2] => invalid i tried following
str.split("\"|") but there quote in beginning
"invalid exp date "approved what best way parse such string?
this straightfoward solution using string#gsub , string#split:
s = "\"approved\"|\"222222\"|\"11111111\"|\"\"|\"m\"|\"\"|\"5454\"|\"mc\"" s.gsub('"','').split('|') #=> ["approved", "222222", "11111111", "", "m", "", "5454", "mc"]
Comments
Post a Comment