1

I need to replace the {0} with values from hashmap. hashmap key is 0,1,2,3 respectively. what would be the right option.i feel we can implement Pattern matcher. What is the pattern for this value?

Dear {0}, 
You are being contacted because the '{1}' named '{2}' has been changed by '{3}' in the Application. These changes may impact any existing reports you are using which depend upon this information. If you have not authorized these changes, please contact '{4}' or send a request to  IT Support to have the changes reversed 

Output:

Dear abc , 
You are being contacted because the ' Attribute' named 'prod1_group' has been changed by ' Guest ' in the Application. These changes may impact any existing reports you are using which depend upon this information. If you have not authorized these changes, please contact 'Guest' or send a request to IT Support to have the changes reversed 

3 Answers 3

2

You can use this regex:

Pattern p = Pattern.compile("\\{(\\d+)}");

And use matcher.group(1) as key to your HashMap in a while (matcher.find()) {..} loop.

Where matcher is:

Matcher matcher = p.matcher( input );

RegEx Demo

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

3 Comments

This will fail with PatternSyntaxException, you have to escape { character.
Thanks @iMmo: I had \\{ and \\} earlier but then it worked without error on regex101 site so I removed it :)
I saw it, but regex101 doesnt support java, btw dont need to escape } :)
0

You need to ecsape { character otherwise you will get PatternSyntaxException and in order to capture digits \\d+. Lastly matcher.group(1) will return String , so you need to cast it to Integer Below is a sample

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

public class Registrar {
    public static void main(String[] args) {

        String input = "Dear {0}, \n" +
                "You are being contacted because the '{1}' named '{2}' has been changed by '{3}' in the Application. These changes may impact any existing reports you are using which depend upon this information. If you have not authorized these changes, please contact '{4}' or send a request to IT Support to have the changes reversed";


        Pattern pattern = Pattern.compile("\\{(\\d+)}");

        HashMap<Integer, String> map = new HashMap<>();
        map.put(0, "zero");
        map.put(1, "one");
        map.put(2, "two");
        map.put(3, "three");
        map.put(4, "four");

        Matcher matcher = pattern.matcher(input);

        while (matcher.find()) {
            String val = matcher.group(1);
            String group = matcher.group();
            input = input.replace(group, map.get(Integer.parseInt(val)));
        }

        System.out.println(input);
    }
}

And it outputs

Dear zero, 
You are being contacted because the 'one' named 'two' has been changed by 'three' in the Application. These changes may impact any existing reports you are using which depend upon this information. If you have not authorized these changes, please contact 'four' or send a request to IT Support to have the changes reversed

Comments

0

You could do it with \\{[0-9]{1}}

    String testString = "{0}";
    String myPattern = "\\{[0-9]{1}}";
    Pattern pattern = Pattern.compile(myPattern);
    Matcher m = pattern.matcher(testString);
    if(m.matches()) {
        System.out.println("Correct Value");
    } else {
        System.out.println("Wrong Value");          
    }

To match it from a String do like the following

    String testString = "Dear {0},";
    String myPattern = ".*\\{[0-9]{1}}.*";

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.