Java regex for removing all characters except a pattern

让人想犯罪 __ 提交于 2021-01-28 14:42:05

问题


I have a string including e-mail. There are probably extra characters before and / or after it. input examples:

a1@b.com
a2@b.com abcd efg
x y z a3@b.com
p q a4@b.com x z
asd[x5@c.net]gh

I want to remove the extra characters.

Desired outputs:

a1@b.com
a2@b.com
a3@b.com
a4@b.com
x5@c.net

Valid characters are a-zA-Z0-9._ So there are probably invalid characters before and / or after e-mail.

I tried this code to identify whether it is a correct email or not (this assumes that it is separated from extra characters by space), but I can not replace to the desired string (using s.replaceAll()):

if (s.matches("(?i).*\\s[a-zA-Z_\\.]+@[a-zA-Z_\\.]+\\.[a-zA-Z_\\.]+.*") ||
    fields[2].matches("(?i).*[a-zA-Z_\\.]+@[a-zA-Z_\\.]+\\.[a-zA-Z_\\.]+\\s.*"))

回答1:


you can use java.util.regex.Pattern and java.util.regex.Matcher

This code will do what you ask for:

public static void main(String[] args) {
    String[] testList = {"a1@b.com", 
            "a2@b.com abcd efg", 
            "x y z a3@b.com", 
            "p q a4@b.com x z", 
            "asd[a5@b.coom]gh"};

    Pattern EMAIL_PATTERN = Pattern.compile("[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})");


    for(String test : testList){
        Matcher m = EMAIL_PATTERN.matcher(test);
        while (m.find()) {
             System.out.println(m.group(0));
        }
    }
}



回答2:


Given your definition of valid characters, try:

^.*?([\w.]+@[\w.]+).*$

and replace with capturing group 1




回答3:


A validation of email addresses is not possible. It is only possible to validate an email-adress-like-appearence - and even this task is quite tricky, due to new tlds with more than 3 characters.

So, you better find "invalid" email-adresses (mail sending will fail), then missing a valid one.

Use

([a-zA-Z0-9!#$%&'*+-/=?^_`{|}~.]+\@(?:[a-zA-Z0-9.-]+|\[[0-9.]+\]))

to grab anything that could be an email address.

  ([a-zA-Z0-9!#$%&'*+-/=?^_`{|}~.]+\@(?:[a-zA-Z0-9.-]+|\[[0-9.]+\]))

Regular expression visualization

Debuggex Demo



来源:https://stackoverflow.com/questions/25556132/java-regex-for-removing-all-characters-except-a-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!