Suppose you’re dealing with data organized in the following format:
[123]="some string" [234]=999999999 [345]="some other string"
In Java, what would be the easiest way to split each by KV combination, where K is the tag (enclosed in [ ]).
Is there a Utils (Apache or other?) method you know off that lets you define a structure as shown above to help with iterating over it, instead of having to manually count and read data past and between [ ]?
What do we know?
- Each element will have a tag
- Each tag will be surrounded by
[ ] - Tag and value assigned to the tag will be separated by
= - Value will be there. No empty values allowed.
- Value may or may not be surrounded by quotes
Use a
Patternwith regex"\\[(.*?)\\]=(.*?)( (?=\\[)|$)"to grab pairs at a time.The only limitation is that the next term is assumed to start at a space followed by a
[, so this character sequence may not appear within a value.This code demonstrates:
Output: