简体   繁体   中英

Regular expression to remove specific characters in email addresses

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 ?

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

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.

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, ""));
 }
}

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);
   }

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);

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]}`)

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