i have string
string line = "abc:xyz uvw, def:ghi, mno:rst, ijk:efg, abc opq"; i want parse string 2 lists:
arraylist<string> tags; arraylist<string> values; where tags words before colon (in example: abc, def, ijk , mno). want
tags = arrays.aslist("abc", "def", "mno", "ijk"); values = arrays.aslist("xyz uvw", "ghi", "rst", "efg, abc opq"); note values can have spaces , commas in them , not 1 word.
since values can contain commas, need split when find key.
a key defined word preceding :.
so, split pattern ", (?=[a-za-z]+:)" checks comma space chars colon in specified order, looking ahead chars , colon.
checks key, , splits lookahead (thus leaving key intact). give array of keyvalue pairs
then can split : keys , values.
string str = "your string"; string[] keyvaluepairs = str.split(", (?=[a-za-z]+:)"); (string keyvaluepair : keyvaluepairs) { string[] keyvalue = keyvaluepair.split(":"); keysarray.add(keyvalue[0]); valuesarray.add(keyvalue[1]); }
Comments
Post a Comment