简体   繁体   English

JAVA Regex - 如何排除某个电子邮件扩展名?

[英]JAVA Regex - How do I exclude a certain email extension?

My program is used to sort out emails and find ones without the proper extension.我的程序用于整理电子邮件并查找没有正确扩展名的电子邮件。 For this, I am experimenting in Regex and can get it to detect when an email has the extension, or no extension at all, but can not get the program to detect when the line has an extension that just is not the specific one I wish to exclude.为此,我正在 Regex 中进行试验,并且可以让它检测电子邮件何时具有扩展名,或者根本没有扩展名,但是无法让程序检测到该行的扩展名何时不是我希望的特定扩展名排除。

I have tried using tags like ?!我试过使用像 ?! with the statements and have had no results.与声明并没有结果。 I have not got a lot of experience in regex so my attempts are numbered.我在正则表达式方面没有很多经验,所以我的尝试数不胜数。

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Emails {
    public static void main(String args[]) throws IOException {
        Scanner scanner = new Scanner(new File("rajeev.dat"));

        ArrayList<String[]> lines = new ArrayList<>();

        Pattern regex = Pattern.compile("(?!^([A-Za-z0-9.]+(?!@Google.org)|[A-Za-z0-9.]+(?!@Yahoo.net)))");
        Pattern findComma = Pattern.compile(",");

    while(scanner.hasNextLine()){
        lines.add(scanner.nextLine().split(","));
    }

    for(String[] s: lines){
        for(String s1: s){
            System.out.println(s1);
        }
        System.out.println();
    }


    String temp = "";

    String output = "";

    output += lines.get(0)[0] + ":" + lines.get(0)[1] + ":";

    for(int i = 2; i < lines.get(0).length; i++){
        temp += lines.get(0)[i] + " ";
    }

        System.out.println(temp);

    Matcher match = regex.matcher(temp);
    String temp2 = "";
    boolean nofail = false;

        while(match.find()){
            output += match.group().trim() + ":";
            nofail = true;
        }


        if(nofail) {
            System.out.println(output);
        }


    }
}

The program is expected to sort out any email with extensions that are not @Google.org or @yahoo.net该程序预计会整理任何带有非@Google.org 或@yahoo.net 扩展名的电子邮件

The program finds no matches程序未找到匹配项

Can you try this?你能试试这个吗? https://regex101.com/r/hbnvr8/1/ https://regex101.com/r/hbnvr8/1/

Pattern.compile("^([A-Za-z0-9.]+(\\.?)+([A-z0-9]?))+@(?!(Yahoo.net|Google.org)$)[A-z]+\\.[A-z]+$")

让这个模式在 regextester 上工作:

^([A-z0-9]+(\.?)+([A-z0-9]?))+@(?!(Yahoo.net|Google.com))[A-z]+\.[A-z]+$

You could use a negative lookahead (?!Google\\.org|Yahoo\\.net) to assert what is directly on the right of the @ is not either Google.org or Yahoo.net.您可以使用否定前瞻(?!Google\\.org|Yahoo\\.net)来断言@右侧的内容既不是 Google.org 也不是 Yahoo.net。 Note to escape the dot to match it literally.注意转义点以匹配它的字面意思。

If the only characters you want to allow are listed in your character class [A-Za-z0-9.] , you might use a regex which first matches the character class without the dot using [A-Za-z0-9]+如果您想要允许的唯一字符列在您的字符类[A-Za-z0-9.] ,您可以使用一个正则表达式,它首先使用[A-Za-z0-9]+匹配不带点的字符类

Then repeat the part 0+ times starting with a dot using (?:\\.[A-Za-z0-9])* to prevent the email starting or ending with a dot.然后使用(?:\\.[A-Za-z0-9])*以点开头重复部分 0+ 次,以防止电子邮件以点开头或结尾。

Note that you can extend the character classes to allow more characters.请注意,您可以扩展字符类以允许更多字符。

^[A-Za-z0-9]+(?:\.[A-Za-z0-9])*@(?!Google\.org|Yahoo\.net)[A-Za-z0-9]+(?:\.[A-Za-z0-9])*\.\w+$

In Java在 Java 中

String regex = "^[A-Za-z0-9]+(?:\\.[A-Za-z0-9])*@(?!Google\\.org|Yahoo\\.net)[A-Za-z0-9]+(?:\\.[A-Za-z0-9])*\\.\\w+$";

Regex demo正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM