I am trying to extract the name of a property referenced in a string using the $() construct. For instance, if bb=xo-xo, then "aa$(bb)aa" expands to "aaxo-xoaa".
Here is the code:
public static void main(String[] args) {
final String PROPERTY_NAME_REGEX = "\\w+(?:\\.\\w+)*";
final String PROPERTY_REFERENCE_REGEX = "\\$\\((" + PROPERTY_NAME_REGEX + ")\\)";
Pattern pattern = Pattern.compile(PROPERTY_REFERENCE_REGEX);
String value = "hhh $(aa.bbcc.dd) @jj $(aakfd) j";
Matcher matcher = pattern.matcher(value);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
System.out.println(String.format("\"%s\" at [%d-%d)",
matcher.group(),
matcher.start(),
matcher.end()));
for (int i = 0; i < matcher.groupCount(); ++i) {
System.out.println(String.format("group[%d] = %s", i, matcher.group(i)));
}
}
}
And it displays:
"$(aa.bbcc.dd)" at [4-17)
group[0] = $(aa.bbcc.dd)
"$(aakfd)" at [22-30)
group[0] = $(aakfd)
But I was hoping to get the following output:
"$(aa.bbcc.dd)" at [4-17)
group[0] = aa.bbcc.dd
"$(aakfd)" at [22-30)
group[0] = aakfd
What am I doing wrong?
ExpandPropertiesclass from ant, which does exactly this type of replacements.