0

Im trying to figure out how i can remove certain characters in an email address before the domain name using nothing but a simple regex and replaceAll in Java.

In email addresses,

  • Need to remove any number of . before @<domain name>
  • Also remove anything between + up to @ but not including @. For instance in joebloggs+123@domain.com should be joebloggs@domain.com.

So far I have,

class Main {
  public static void main(String[] args) {
    String matchingRegex = "(\\.|(\\+.*(?=@)))";
    System.out.println("joe.bloggs+123@gmail.com".replaceAll(matchingRegex, ""));
  }
}

which replaces everything including the domain name. joebloggs@gmailcom

What i really need is joebloggs@gmail.com. Can this be achieved with regex alone ?

nixgadget
  • 6,983
  • 16
  • 70
  • 103
  • That's because the beginning of your regex accounts for *all* periods: `\\.|otherStuff` is saying "Periods OR other stuff". Since periods *after* the @ fit the regex (due to that first statement of `\\.` followed by a `|`), they're also being removed. – Vince Dec 05 '19 at 03:35
  • @VinceEmigh Anyway to fix it with the look ahead ? – nixgadget Dec 05 '19 at 03:39
  • Escaped `+` to handle `+123` with that look ahead – nixgadget Dec 05 '19 at 03:40
  • 1
    Oh, my bad, didn't see it in the test string at first. Still, your regex says "Periods OR a + followed by *any character* behind a @". That first expression, the `\\.` followed by a pipe, is what's causing all your periods to be replaced. – Vince Dec 05 '19 at 03:42
  • @VinceEmigh Yup definitely. Is there anyway for that first part to say search up to `@` ? – nixgadget Dec 05 '19 at 03:45

6 Answers6

2

Another look ahead did the trick in the end.

class Main {
  public static void main(String[] args) {
    String matchingRegex = "((\\.+)(?=.*@)|(\\+.*(?=@)))";
    System.out.println("joe.bloggs+123@gmail.com".replaceAll(matchingRegex, ""));
    System.out.println("joebloggs+123@gmail.com".replaceAll(matchingRegex, ""));
    System.out.println("joe.bloggs@gmail.com".replaceAll(matchingRegex, ""));
    System.out.println("joe.bloggs.123@gmail.com".replaceAll(matchingRegex, ""));
    System.out.println("joe.bloggs.123+456@gmail.com".replaceAll(matchingRegex, ""));
    System.out.println("joebloggs@gmail.com".replaceAll(matchingRegex, ""));
    System.out.println("joe.bloggs.123+456.789@gmail.com".replaceAll(matchingRegex, ""));
  }
}

Results in,

joebloggs@gmail.com
joebloggs@gmail.com
joebloggs@gmail.com
joebloggs123@gmail.com
joebloggs123@gmail.com
joebloggs@gmail.com
joebloggs123@gmail.com
nixgadget
  • 6,983
  • 16
  • 70
  • 103
1

You could try spliting the string (the email) on the @ and running replaceAll on the the first half and then put the strings back together.

Check out: How to split a string in Java

For splitting strings.

notansandwich
  • 59
  • 1
  • 7
  • Or he could fix his regex. Although this is answer may work, it creates overhead which could easily be avoided if using the proper regular expression. – Vince Dec 05 '19 at 03:36
  • Splitting would be easy but like i said it should be a regex and `replaceAll`. – nixgadget Dec 05 '19 at 03:38
1

Try this regex [.](?=.*@)|(?=\\+)(.*)(?=@). It looks up dots up to @ (even if there's text in between), or everything from + up to @. Hope it helps https://regex101.com/r/gyUpta/1

class Main {
 public static void main(String[] args) {

    String matchingRegex = "[.](?=.*@)|(?=\\+)(.*)(?=@)";
    System.out.println("joe.bloggs+123@gmail.com".replaceAll(matchingRegex, ""));
 }
}
Gabriel Macus
  • 444
  • 1
  • 4
  • 9
0

This will do the trick...

public static void main(String args[]) {
       String matchingRegex = "(\\.|(\\+.*(?=@)))";
       String email = "joe.bloggs+123@gmail.com";
       String user = email.substring(0, email.indexOf("@")+1);
       String domain = email.substring(email.indexOf("@")+1);          
       System.out.println(user.replaceAll(matchingRegex, "") + domain);
   }
Rowi
  • 545
  • 3
  • 9
0

This is the easiest way I have found to do it.

      String address = "joe.bloggs+123@gmail.com";
      int at = address.indexOf("@");

      address = address.substring(0, at).replaceAll("\\.|\\+.*", "")
            + address.substring(at);

      System.out.println(address);
WJS
  • 36,363
  • 4
  • 24
  • 39
0

if you try to split for regex sorry i don't remember java this example is in javascript

let string = "joe.bloggs+123@gmail.com"

//firts the function
function splitString(params) {
    return params.split(/\+(.)+\@/)
}

//second the concat
let list = splitString(string)
//     the first element+the las element 
console.log(`${list[0]}${list[list.length -1]}`)