Using Java, I want to extract the words between the dollarsign symbol $.
For example:
String = " this is first attribute $color$. this is the second attribute $size$"
I want to pull out the strings: color and size and put them into a List.
I have tried:
Pattern pattern = Pattern.compile("(\\$) .* (\\$)");
Matcher matcher = pattern.matcher(sentence);
but I get the output:
"$color$.this is the second attribute $size$"
What is the best way to do this?
The problem is that the regex you are using is greedy and consumes everything beginning at the first
$until the last$in your string. You have to add a?after the*to make the regex nongreedy: