Home > Software engineering >  How should I get the domain name without extension from email? [closed]
How should I get the domain name without extension from email? [closed]

Time:09-30

I would like to extract the word domain from

info@domain.fr

I know I could split by @, then get the second element, split again with . and take the first element, but I guess there is a much cleaner way to do it. A regexp ? Maybe something ever simpler ?

CodePudding user response:

Both parsing and Regex are possible. If you pick one, it would be great if you follow this answer that lists all the possible characters of a valid URL. So this Regex might be what are you looking for.

@([A-Za-z0-9-._~:\/\?#\[\]@!$&'\(\)\*\ ,;%=] )\.

CodePudding user response:

Try (?<=@). (?=\.)

Outputs:

domain

Regex101 Demo

CodePudding user response:

public class TestClass {
public static void main(String args[]) {
    
    Pattern p = Pattern.compile("\\@(.*?)\\.");
    Matcher m = p.matcher("[email protected]");
    if (m.find()) {
        System.out.println(m.group(1));
    }
  }
}
  • Related