[英]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.我试图弄清楚如何在域名之前删除电子邮件地址中的某些字符,只使用简单的正则表达式和 Java 中的replaceAll
。
In email addresses,在电子邮件地址中,
.
需要删除任意数量的.
before @<domain name>
在@<domain name>
+
up to @
but not including @
.还要删除+
到@
之间的任何内容,但不包括@
。 For instance in joebloggs+123@domain.com
should be joebloggs@domain.com
.例如在joebloggs+123@domain.com
应该是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
.我真正需要的是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.您可以尝试拆分 @ 上的字符串(电子邮件)并在前半部分运行 replaceAll,然后将字符串重新组合在一起。
Check out: How to split a string in Java查看: 如何在 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希望它有帮助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如果您尝试拆分正则表达式,对不起,我不记得 java 这个例子是在 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]}`)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.