The answers proposed so far seem to have missed your intention of combining both ideas into one regex. For sure, it's simpler to use two. However it can be done by using matcher groups and collecting only data from the group we are interested in.
Java 8 version:
public static void main(String[] args) {
Pattern p = Pattern.compile("([a-zA-Z]+)[^a-zA-Z@]*(@.*)?");
String input="user.sure_name123@mail.co";
System.out.println(MatcherStream.results(p, input)
.map(result -> result.group(1))
.collect(Collectors.joining(" ")));
// MatcherStream implementation http://stackoverflow.com/a/42462014/7098259
}
Java 9 version:
It's more convenient to stream the match results in Java 9.
public static void main(String[] args) {
System.out.println(Pattern.compile("([a-zA-Z]+)[^a-zA-Z@]*(@.*)?")
.matcher("user.sure_name123@mail.co").results()
.map(result -> result.group(1))
.collect(Collectors.joining(" ")));
}
replaceAll version:
Finally, this one isn't a pure regex solution, since it requires you to trim off an extra space character at the end. But as you can see it's much more concise to use replaceAll:
public static void main(String[] args) {
String input = "user.sure_name123@mail.co";
System.out.println(input.replaceAll("((@.*)|[^a-zA-Z])+", " ").trim());
}
Output:
user sure name