java - Is there a simple way of parsing this text into a Map -



java - Is there a simple way of parsing this text into a Map -

i receive response service below. how parse map? first thought of split @ whitespace doesn't work value might contain spaces e.g. @ value of sa key in below response.

one alternative thought of split @ whitespace provided previous character double quote. not sure how write regex though.

tx="0000000000108000001830001" fi="" os="8" ci="qu01sf1s2032" aw="sss" sa="1525 windward concourse"

parse @ quotes. utilize regular look find each key/value pair, assuming each value in quotes. question be, rules if value contains embedded quotes? (are escaped using '\' or such? regardless, not accounted in below...)

for example:

(\w+)="([^"]*)"

this give groups #1 , #2 can used provide key , value, respectively.

run in loop, using java's matcher.find() method, until find of pairs.

sample code:

string input = "tx=\"0000000000108000001830001\" fi=\"\" os=\"8\" ci=\"qu01sf1s2032\" aw=\"sss\" sa=\"1525 windward concourse\""; pattern p = pattern.compile("\\s*(\\w+)=\"([^\"]*)\"\\s*"); matcher m = p.matcher(input); while(m.find()){ system.out.println(m.group(1)); system.out.println(m.group(2)); }

output:

tx 0000000000108000001830001 fi os 8 ci qu01sf1s2032 aw sss sa 1525 windward concourse

java regex algorithm parsing groovy

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -