Question

I have a string in format:

<+923451234567>: Hi here is the text.

Now I want to get the mobile number(without any non-alphanumeric characters) ie 923451234567 in the start of the string in-between < > symbols, and also the text ie Hi here is the text.

Now I can place a hardcoded logic, which I am currently doing.

String stringReceivedInSms="<+923451234567>: Hi here is the text.";

String[] splitted = cpaMessage.getText().split(">: ", 2);
String mobileNumber=MyUtils.removeNonDigitCharacters(splitted[0]);
String text=splitted[1];

How can I neatly get the required strings from the string with regular expression? So that I don't have to change the code whenever the format of the string changes.

Was it helpful?

Solution

String stringReceivedInSms="<+923451234567>: Hi here is the text.";

Pattern pattern = Pattern.compile("<\\+?([0-9]+)>: (.*)");
Matcher matcher = pattern.matcher(stringReceivedInSms);
if(matcher.matches()) {
    String phoneNumber = matcher.group(1);
    String messageText = matcher.group(2);
}

OTHER TIPS

Use a regex that matches the pattern - <\\+?(\\d+)>: (.*)

Use the Pattern and Matcher java classes to match the input string.

Pattern p = Pattern.compile("<\\+?(\\d+)>: (.*)");
Matcher m = p.matcher("<+923451234567>: Hi here is the text.");
if(m.matches())
{
    System.out.println(m.group(1));
    System.out.println(m.group(2));
}

You need to use regex, the following pattern will work:

^<\\+?(\\d++)>:\\s*+(.++)$

Here is how you would use it -

public static void main(String[] args) throws IOException {
    final String s = "<+923451234567>: Hi here is the text.";
    final Pattern pattern = Pattern.compile(""
            + "#start of line anchor\n"
            + "^\n"
            + "#literal <\n"
            + "<\n"
            + "#an optional +\n"
            + "\\+?\n"
            + "#match and grab at least one digit\n"
            + "(\\d++)\n"
            + "#literal >:\n"
            + ">:\n"
            + "#any amount of whitespace\n"
            + "\\s*+\n"
            + "#match and grap the rest of the string\n"
            + "(.++)\n"
            + "#end anchor\n"
            + "$", Pattern.COMMENTS);
    final Matcher matcher = pattern.matcher(s);
    if (matcher.matches()) {
        System.out.println(matcher.group(1));
        System.out.println(matcher.group(2));
    }
}

I have added the Pattern.COMMENTS flag so the code will work with the comments embedded for future reference.

Output:

923451234567
Hi here is the text.

You can get your phone number by just doing :

stringReceivedInSms.substring(stringReceivedInSms.indexOf("<+") + 2, stringReceivedInSms.indexOf(">"))

So try this snippet:

public static void main(String[] args){
        String stringReceivedInSms="<+923451234567>: Hi here is the text.";

        System.out.println(stringReceivedInSms.substring(stringReceivedInSms.indexOf("<+") + 2, stringReceivedInSms.indexOf(">")));
    }

You don't need to split your String.

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