Question

I have a MessageFormat like so;

final MessageFormat messageFormat = new MessageFormat("This is token one {0}, and token two {1}");

I'm just wondering if I have some Strings like;

String shouldMatch = "This is token one bla, and token two bla";
String wontMatch = "This wont match the above MessageFormat";

How do I check if the above Strings were created using messageFormat? I.e. that they match the messageFormat?

Many thanks!

Was it helpful?

Solution

You can do that using Regular Expressions and Pattern and Matcher classes. A simple example:

Pattern pat = Pattern.compile("^This is token one \\w+, and token two \\w+$");
Matcher mat = pat.matcher(shouldMatch);
if(mat.matches()) {
   ...
}

Explanation of regex:

^ = beginning of line
\w = a word character. I put \\w because it is inside a Java String so \\ is actually a \
+ = the previous character can occur one ore more times, so at least one character should be there
$ = end of line

If you want to capture the tokens, use braces like this:

Pattern pat = Pattern.compile("^This is token one (\\w+), and token two (\\w+)$");

You can retrieve the groups using mat.group(1) and mat.group(2).

OTHER TIPS

Apart from the Reg Ex answer it can be done also with this code I have just developed:

public class Points {
    private int start;

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }
    private int end;

    public int getEnd() {
        return end;
    }

    public void setEnd(int end) {
        this.end = end;
    }
}

public class Split {
    public static void main(String[] args) {
        final MessageFormat messageFormat = new MessageFormat("This is token one {0}, and token two {1}");

        ArrayList<String> list = new ArrayList<String>();
        list.add("This is token one bla, and token two bla");
        list.add("This wont match the above MessageFormat");
        list.add("This wont match the above MessageFormat");
        list.add("This wont match the above MessageFormat");
        list.add("This is token one bla, and token two bla");
        list.add("This wont match the above MessageFormat");

        Format[] format = messageFormat.getFormats();
        int del = format.length;

        ArrayList<String> delimeters = new ArrayList<String>();
        for (int i = 0; i < del; i++) {
            delimeters.add("{" + i + "}");
        }
        //System.out.println(messageFormat.toPattern());
        ArrayList<Points> points = new ArrayList<Points>();
        int counter = 0;
        for (String x : delimeters) {
            Points tmp = new Points();
            tmp.setStart(counter);
            int ending = messageFormat.toPattern().indexOf(x);
            counter = ending + x.length();
            tmp.setEnd(ending);
            points.add(tmp);
        }

        for (String x : list) {
            boolean match = true;
            for (Points y : points) {
                if (match) {
                    if (x.substring(y.getStart(), y.getEnd()).equals(messageFormat.toPattern().substring(y.getStart(), y.getEnd()))) {
                    } else {
                        match = false;
                    }
                }
            }
            if (match) {
                System.out.println("Match!!!");
            } else {
                System.out.println("Not Match!!");
            }
        }

    }
}

Running this will print you the output

Match!!!

Not Match!!

Not Match!!

Not Match!!

Match!!!

Not Match!!

Hope you like it!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top