Java Reg Exp for Word后面没有其他单词

时间:2016-06-25 23:44:39

标签: java regex string

基本上,我在java中编写一个程序,我必须在三个桶中的任何一个中对String进行分类。

  • 类别1 - 包含“AND”和“AND NOT”的字符串
  • 类别2 - 带有'AND NOT'的字符串
  • 类别3 - 带有'AND'的字符串

我需要一些正则表达式来匹配字符串,如果不跳过,则匹配带有AND的字符串。

A AND B AND NOT C - Fail
A AND B AND C - Fail
A AND NOT B AND NOT C - Pass

以下是示例代码段

public static void main(String[] args) {
    String X = "A AND B AND C AND D AND NOT E";
    String Y = "A AND NOT C ";
    String Z = "A AND B AND D";
    ArrayList<String> sampleString=new ArrayList<String>(Arrays.asList(X,Y,Z));

    //Category 1 - String with both 'AND' and 'AND NOT'
    //Category 2 - String with 'AND NOT' only
    //Category 3 - String with 'AND' only

    for(String s:sampleString){
        if(s.contains("AND") && s.contains("NOT")){
            System.out.println("Category 1 -"+s);
        }
        // This condition is invalid - I need some regex to match this condition. I need to consider only AND followed by NOT if not skip

        if(s.contains("AND NOT") && !s.contains("AND")){
            System.out.println("Category 2 - "+s);
        }
        if(s.contains("AND") && !s.contains("NOT")){
            System.out.println("Category 3 - "+s);
        }
    }

输出 -

Category 1 -A AND B AND C AND D AND NOT E
Category 1 -A AND NOT C 
Category 3 - A AND B AND D

我尝试了一些正则表达式问题,但没有解决我的问题。我试过下面的

String regex="AND(?!\\s+NOT)";

public static void main(String args[]){
        String x= "A AND B AND C AND NOT D"; 
        String regex="AND(?!\\s+NOT)";
        if(Pattern.compile(regex).matcher(x).find()){
            System.out.println("X MATCHED");
        }
    } 
//Returns - X MATCHED

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

以下正则表达式find()循环将确定类别,如果输入未匹配任何列出的类别,则返回0。

private static int categorize(String input) {
    Matcher m = Pattern.compile("(?i)\\bAND(\\s+NOT)?\\b").matcher(input);
    boolean foundAndNot = false, foundAnd = false;
    while ((! foundAndNot || ! foundAnd) && m.find())
        if (m.start(1) != -1)
            foundAndNot = true;
        else
            foundAnd = true;
    return (foundAndNot ? (foundAnd ? 1 : 3)
                        : (foundAnd ? 2 : 0));
}

&&循环中while条件的左侧只是一个短路,如果找到这两个条件,则提前退出循环。

正则表达式中的(?i)用于使其不区分大小写,这是正则表达式优于任何contains()实现的地方。

m.start(1) != -1检查是为了查看捕获组是否匹配,即查看匹配是否包含NOT字。

<强> TEST

System.out.println(categorize("A AND B AND NOT C"));     // prints 1
System.out.println(categorize("A AND B AND C"));         // prints 3
System.out.println(categorize("A AND NOT B AND NOT C")); // prints 2
System.out.println(categorize("A OR B OR NOT C"));       // prints 0

答案 1 :(得分:1)

试试这个:

boolean hasBoth = x.matches("(?=.*AND NOT).*AND(?! NOT).*");
boolean onlyAnd = x.matches("(?!.*AND NOT).*AND.*");
boolean onlyAndNot = x.matches("(?!.*AND(?! NOT)).*AND NOT.*");