简体   繁体   中英

Java regex for removing all characters except a pattern

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.*"))

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

Given your definition of valid characters, try:

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

and replace with capturing group 1

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.]+\]))

正则表达式可视化

Debuggex Demo

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