Get string with @ in between using regex for email - Java Regular Expressions

Java examples for Regular Expressions:Email

Description

Get string with @ in between using regex for email

Demo Code

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

public class Main {
    public static void main(String[] args) {
        String input = "asdf@asdf.bet";

        //proccesing ...
        Pattern pattern = Pattern.compile("([a-zA-Z.-_]+)@([a-zA-Z-]+\\.[a-zA-Z]+)");
        Matcher matcher = pattern.matcher(input);

        while (matcher.find()) {
            System.out.println(matcher.group(1) + "@" + matcher.group(2));  // split & print user@host
        }/*from w  ww .j  a va  2s .c  o m*/
    }
}

Related Tutorials