简体   繁体   中英

how do I split email from a String

I have a string "Test Test <test@test.com>" I need to split the email address only if the String is present in this format or else no need to spilt. I have not got any solutions for this to show my approach.

I need to split email address only if present in the above mentioned format.

You can extract email like this.

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

public class HelloWorld {

    public static void main( String[] args ) {
        String mydata ="Test Test <test@test.com>";
        Pattern pattern = Pattern.compile(".*<(.*)>");
        Matcher matcher = pattern.matcher(mydata);

        String email = null;
        if (matcher.find())
        {
            email = matcher.group(1);

        }
         System.out.println(email);
    }
}

Try this:

    String address ="Test Test <test@test.com>";
    String emailAddress = address.replaceFirst(".*<(.*)>", "$1");
    System.out.println(emailAddress);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM