Using Java and regex, I want to extract strings from a line of text. The text can be in following format -
- key1(value1) key2(value2)
- key1(value1) key2
- key1 key2(value2)
- key1 key2
- key1
Am successfully able to extract the keys and values when Type #1 is used where I can split the text using space and then use following pattern to extract keys
Pattern p = Pattern.compile("\\((.*?)\\)",Pattern.DOTALL);
A complicated code logic for counting the occurance of "(" and matching it with occurence of the space can be used for Case #2 and Case #3, however, the code becomes way too long. Multiple complication arise when spaces are present in values too because then, splitting text becomes problematic.
Is there a better regex splitting/holiding I can use for selective Cases depicted above?