1

I know there are similar questions regarding to this. However, I tried many solutions and it just does not work for me.

I need help to extract multiple substrings from a string:

String content = "Ben Conan General Manager 90010021 [email protected]";

Note: The content in the String may not be always in this format, it may be all jumbled up.

I want to extract the phone number and email like below:

1. 90010021

2. [email protected]

In my project, I was trying to get this result and then display it into 2 different EditText. I have tried using pattern and matcher class but it did not work.

I can provide my codes here if requested, please help me ~

--------------------EDIT---------------------

Below is my current method which only take out the email address:

private static final String EMAIL_PATTERN =
            "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
                        "\\@" +
                        "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
                        "(" +
                            "\\." +
                            "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
                        ")+";



public String EmailValidator(String email) {

        Pattern pattern = Pattern.compile(EMAIL_PATTERN);
        Matcher matcher = pattern.matcher(email);

        if (matcher.find()) {

            return email.substring(matcher.start(), matcher.end());

        } else {

            // TODO handle condition when input doesn't have an email address

        }

        return email;
    }
5
  • 2
    split string using comma Commented Jan 29, 2016 at 6:13
  • @Vivek Mishra Hi, the text does not necessary has the commas. I just place it there for easier viewing. I have a regex formula for email and phone tho, but its not working:) Will remove the commas now Commented Jan 29, 2016 at 6:16
  • then use space to separate it Commented Jan 29, 2016 at 6:17
  • String s = Ben Conan General Manager.split("")); String s1 = 90010021.split("")); String s2 = [email protected]("")); Now use your s1 & s2 Commented Jan 29, 2016 at 6:23
  • @AmarbirSingh have you seen what you have posted? Commented Jan 29, 2016 at 6:29

4 Answers 4

2

You can separate your string into arraylist like this

    String str = "Ben Conan, General Manager, 90010021, [email protected]";
List<String> List = Arrays.asList(str.split(" "));
Sign up to request clarification or add additional context in comments.

Comments

0

maybe you should do this instead of yours :

String[] Stringnames = new String[5]

Stringnames [0] = "your phonenumber"
Stringnames[1] = "your email"

System.out.println(stringnames)

Or :

String[] Stringnames = new String[2]
String[] Stringnames = {"yournumber","your phonenumber"};

System.out.println(stringnames [1]);

Comments

0

String.split(...) is a java method for that.

EXAMPLE:

String content = "Ben Conan, General Manager, 90010021, [email protected]";
String[] selection = content.split(",");
System.out.println(selection[0]);
System.out.println(selection[3]);

BUT if you want to do a Regex then take a look at this:

https://stackoverflow.com/a/16053961/982161

4 Comments

Hi, the text does not necessary has the commas. I just place it there for easier viewing. Sorry for the confusion. I have regex formula for email and phone, but its not working :(
ummm...place the right string so I can suggest something that works,
Hi , sorry for the confusion, I have updated my answer :)
@MattDonalds, ok tnxs, take a look at my updated answer please
0

Try this regex for phone number

[\d+]{8}  ---> 8 represents number of digits in phone number

You can use

[\d+]{8,} ---> if you want the number of more than 8 digits

Use appropriate JAVA functions for matching. You can try the results here http://regexr.com/

For email, it depends whether the format is simple or complicated. There is a good explanation here

http://www.regular-expressions.info/index.html

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.