I've got a string like this: "Hi $namevar$ how are you?". I want to:
For 1) I'm using the following code:
String my = "hi $namevar$ how are you?";
Matcher m = Pattern.compile("\\$(\\w.*?)\\$").matcher(my);
while (m.find()) {
System.out.println(m.group(1));
}
Any tips for 2) and 3)?
Here's a more generic approach that doesn't require you to provide a pattern, works for multiple placeholders and can match a placeholder over multiple words too. Say, we have the following template and input string:
String input = "Hi John Doe, how are you? I'm Jane.";
String template = "Hi $namevar$, how are you? I'm $namevar2$.";
So, we parse all the placeholder names and add them to a Map
first. We use a Map
so that we can assign the actual String
values as keys later on.
Matcher m = Pattern.compile("\\$(\\w*?)\\$").matcher(template);
Map<String, String> vars = new LinkedHashMap<String, String>();
while (m.find()) {
System.out.println(m.group(1));
vars.put(m.group(1), null);
}
Now, we generate the pattern to parse placeholder values as
String pattern = template;
// escape regex special characters
pattern = pattern.replaceAll("([?.])", "\\\\$1");
for (String var : vars.keySet()) {
// replace placeholders with capture groups
pattern = pattern.replaceAll("\\$"+var+"\\$", "([\\\\w\\\\s]+?)");
}
Now, we make a match and capture the placeholder values. The values are then stored in the same Map
created above, assigned to their corresponding placeholder variables.
m = Pattern.compile(pattern).matcher(input);
if (m.matches()) {
int i = 0;
for (String var : vars.keySet()) {
vars.put(var, m.group(++i));
}
}
Now, let's print out our captured values by iterating over the Map
as
for (Map.Entry<String, String> entry : vars.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
Output :
namevar = John Doe
namevar2 = Jane
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With