Jothi
Jothi

Reputation: 15090

Java pattern matching for {0}

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 

Upvotes: 1

Views: 98

Answers (3)

anubhava
anubhava

Reputation: 785108

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

Upvotes: 2

RahulArackal
RahulArackal

Reputation: 954

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}}.*";

Upvotes: 0

sol4me
sol4me

Reputation: 15698

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

Upvotes: 0

Related Questions