I am a noob to regex.
I have string like:-
String str = "sbs 01.00 sip ${dreamworks.values} print ${fwVer} to
used ${lang} en given ${model} in ${region}";
and
i have to extract all patterns matched with this type ${….}
Like:- for given str result should be
${dreamworks.values}
${fwVer}
${lang}
${model}
${region}
further if it finds any duplicates then gives only one .
for ex:-
String feed = "sip ${dreamworks.values} print ${fwVer} to ${fwVer} used
${lang} en ${lang}given ${model} in ${region}"
result should be:-
${dreamworks.values}
${fwVer}
${lang}
${model}
${region}
only
this is my answer:-
PLACEHOLDER_PATTERN = "\\$\\{\\w+\\}";
but this one not giving the correct result.
it gives only
${fwVer}
${lang}
${model}
${region}
So please suggest me correct regex.
You are not considering the
.in between the word.\\wdoes not include thedot(.).You need to change your pattern to: –
dot(.)matches everything, and that is what you want right?Also, I have used here
reluctantquantifier –.+?so that it only matches the first}after{, since if you use a greedy quantifier(.+),dot(.)will also match the}in the way till it finds the last}.UPDATE: –
To get just the unique values, you can use this pattern: –
It will match only those pattern, which is not followed by the string containing the same pattern.
NOTE: – Here, I have used
[^}], in place of.+?. It will match any character except}. So, now in this case, you don’t need areluctantquantifier.\1is used forbackreferencing, but we need to escape it with a backslash, and hence\\1, and(?!...)is used fornegative look ahead.