1

I'm trying to get a user name from his email. I know that there are easy ways to achieve this, but it got me wondering if i could achieve this using only regex.

Let's suppose that the user enters the following email: user.sure_name123@mail.co

From that string, I want to extract: user sure name

I've tried so far:

([a-zA-Z]+)

but with this, the domain is included. And with .*(?=@) I can get everything before '@'.

I don't know how to combine the two to achieve my goal.

Any tips? Thanks!

jhamon
  • 3,603
  • 4
  • 26
  • 37
Alan
  • 73
  • 2
  • 8

5 Answers5

2

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

Patrick Parker
  • 4,863
  • 4
  • 19
  • 51
0

Use the following:

email.replaceAll("@.*","").replaceAll("[^a-zA-Z]+", " ").trim();

This will effectively remove anything after the @ sign, then in the remaining part will replace all sequences of non alphabetic characters with a single space. In the end, the trim method is called to remove the initial and final spaces in case you had 123 at the end or the beginning of the email address'user part.

Olivier Grégoire
  • 33,839
  • 23
  • 96
  • 137
  • That was it! I was trying to do in one expression, but I couldn't do it. There is just one problem. `[^a-zA-Z]+` is not getting dots and I don't know why. Can you tell me why? Thanks you, very much! – Alan Mar 26 '18 at 15:05
  • @AlanW. What do you mean by "`[^a-zA-Z]+` is not getting dots"? [Here's an online Java interpreter that showcases the code here above.](https://tio.run/##XY67DsIwDEV3vsLKlEIbCRhZ6AcwIRYQIBMi1JKmVeIgHuq3F5fHAEsc32uf6xIvmJXHc9dpiyHAAgsHjwFAEw@20BAIiculLo5QsSeX5At32mwB/Skkr1GA5S2QqVQdSTVsk3XSXMmjplUw3mFlpIj8UyF6s@/78WQ6Z55VuhZJMmNKO@DnE/cOgX/GV@4Xv9HeUPTurSlvGova5NZKMVdDkQqR/IibHWb3PFtvRyIFAewyspKfA9quewI) – Olivier Grégoire Mar 26 '18 at 15:10
0

In Java you can extract the username part of the e-mail with regular expressions using the Matcher class. In order to replace the non-letter and non-number symbols I suggest you use the replaceAll method in the String class after you have extracted the piece of text:

java.util.regex.Pattern p = java.util.regex.Pattern.compile("^([^@]+)");
java.util.regex.Matcher m = p.matcher("user.sure_name123@mail.co");

String userName = null;
if (m.find()) {
    userName = m.group(0).replaceAll("[^a-zA-Z]", " ");
}
Rubms
  • 725
  • 7
  • 16
0

This is my simple solution using regexp pattern with groups:

private static final Pattern EMAIL = Pattern.compile("(?<one>[^\\.]+)\\.(?<two>[^_]+)_(?<three>[^@\\d]+).+");

public static String getName(String email) {
    Matcher matcher = EMAIL.matcher(email);
    return matcher.matches() ? matcher.group("one") + ' ' + matcher.group("two") + ' ' + matcher.group("three") : null;
}

This is a link to Demo at regex101.com

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
0
String email = "user.sure_name123@mail.co";
String result = email.replaceAll("@.+$", ""); //user.sure_name123
result = result.replaceAll("\\W+"," "); //user sure name123
tsogtgerel.ts
  • 955
  • 1
  • 15
  • 32
  • While this code may answer the question, providing additional context regarding **how** and **why** it solves the problem would improve the answer's long-term value. – Alexander Mar 27 '18 at 02:57