1

I am having a string template containing $variables which needs to be replaced.

String Template: "hi my name is $name.\nI am $age old. I am $sex"

The solution which i tried verifying does not work in the java program. http://regexr.com/3dtq1

Further, I referred to https://www.regex101.com/ where i could not check if the pattern works for java. But, while going through one of the tutorials I found that "$ Matches end of line". what's the best way to replace the tokens in the template with the variables?

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternCompiler {
    static String text = "hi my name is $name.\nI am $age old. I am $sex";
    static Map<String,String> replacements = new HashMap<String,String>();
    static Pattern pattern = Pattern.compile("\\$\\w+");
    static Matcher matcher = pattern.matcher(text);

    public static void main(String[] args) {

        replacements.put("name", "kumar");
        replacements.put("age", "26");
        replacements.put("sex", "male");

        StringBuffer buffer = new StringBuffer();

        while (matcher.find()) {
            String replacement = replacements.get(matcher.group(1));
            if (replacement != null) {
                // matcher.appendReplacement(buffer, replacement);
                // see comment 
                matcher.appendReplacement(buffer, "");
                buffer.append(replacement);
            }
        }
        matcher.appendTail(buffer);
        System.out.println(buffer.toString());


    }

}
1
  • That should work fine. You've escaped the $ appropriately (\), so it wont match end of line. Whats the error? Commented Aug 1, 2016 at 15:02

4 Answers 4

3

You are using matcher.group(1) but you didn't define any group in the regexp (( )), so you can use only group() for the whole matched string, which is what you want.

Replace line:

String replacement = replacements.get(matcher.group(1));

With:

String replacement = replacements.get(matcher.group().substring(1));

Notice the substring, your map contains only words, but matcher will match also $, so you need to search in map for "$age".substring(1)" but do replacement on the whole $age.

Sign up to request clarification or add additional context in comments.

5 Comments

not quite right - he doesn't want the whole string, just the word after the dollar
OK, but he needs to replace the text in the string also, so he needs actually both $age and age. He can get either of those by adding or removing the $.
I tied using the static Pattern pattern = Pattern.compile("\\$\\w+"); but it is still not working for me.
@krzyk ah good point. It does still require brackets to capture though "(\\$\\w+)"?
@dataEnthusiast Corrected case to take into account your map content, try again.
1

You can try replacing the pattern string with

\\$(\\w+)

and the variable replacement works. Your current pattern only has group 0 (the entire pattern) but not group 1. Adding the parenthesis makes the first group the variable name and the replacement will replace the dollar sign and the variable name.

Comments

0

Your code has just minor glitches.

    static Map<String,String> replacements = new HashMap<>();
    static Pattern pattern = Pattern.compile("\\$\\w+\\b"); // \b not really needed

            // As no braces (...) there is no group(1)
            String replacement = replacements.get(matcher.group());

Comments

0

Your not using the right thing as your key. Change to group(), and change map to '$name' etc:

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HelloWorld {
    static String text = "hi my name is $name.\nI am $age old. I am $sex";
    static Map<String,String> replacements = new HashMap<String,String>();
    static Pattern pattern = Pattern.compile("\\$\\w+");
    static Matcher matcher = pattern.matcher(text);

    public static void main(String[] args) {

        replacements.put("$name", "kumar");
        replacements.put("$age", "26");
        replacements.put("$sex", "male");

        StringBuffer buffer = new StringBuffer();

        while (matcher.find()) {
            String replacement = replacements.get(matcher.group());
            System.out.println(replacement);
            if (replacement != null) {
                // matcher.appendReplacement(buffer, replacement);
                // see comment 
                matcher.appendReplacement(buffer, "");
                buffer.append(replacement);
            }
        }
        matcher.appendTail(buffer);
        System.out.println(buffer.toString());


    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.