简体   繁体   English

为什么Pattern.matches(“ [a * mn]”,“ aaaa”)返回true? 什么是适当的代码才能获得所需的输出?

[英]Why doesn't Pattern.matches(“[a*mn]”,“aaaa”) return true? What should be proper code to get the desired output?

I want to create a pattern where the desired string should either be multiples of a including null ie a*, or it should be one single m or single n. 我想创建一个模式,其中所需的字符串应为a的倍数,包括null,即a *,或者应为单个m或单个n。 But the following code doesn't give the desired output. 但是以下代码无法提供所需的输出。

class Solution {
            public static void main(String args[]) {
                System.out.println(Pattern.matches("[a*mn]", "aaaa"));
        }
}

* within a character class ( [] ) is just a * , not a quantifier. *字符类内( []仅仅是一个* ,而不是一个量词。

I want to create a pattern where the desired string should either be multiples of a including null ie a*, or it should be one single m or single n. 我想创建一个模式,其中所需的字符串应为a的倍数,包括null,即a *,或者应为单个m或单个n。

You'll need an alternation ( | ) for that: a*|[mn] : 为此,您需要一个替代( | ): a*|[mn]

Pattern.matches("a*|[mn]", "aaaa")

Live example : 现场示例

import java.util.regex.Pattern;

class Example {
    public static void main (String[] args) throws java.lang.Exception {
        check("aaaa", true);
        check("a", true);
        check("", true);
        check("m", true);
        check("n", true);
        check("mn", false);
        check("q", false);
        check("nnnn", false);
    }
    private static void check(String text, boolean expect) {
        boolean result = Pattern.matches("a*|[mn]", text);
        System.out.println(
            (result ? "Match   " : "No match") +
            (result == expect ? " OK    " : " ERROR ") +
            ": " + text
        );
    }
}

...though obviously if you were really using the pattern repeatedly, you'd want to compile it once and reuse the result. ...虽然很明显,如果您真的在重复使用该模式,则希望将其编译一次并重用结果。

Try this regex 试试这个正则表达式

(a*)|m|n
Pattern.matches("(a*)|m|n", "") // true, match 1st group
Pattern.matches("(a*)|m|n", "a") // true, match 1st group
Pattern.matches("(a*)|m|n", "aaaa") // true, match 1st group
Pattern.matches("(a*)|m|n", "m") // true, match `n`
Pattern.matches("(a*)|m|n", "n") // true, match `m`
Pattern.matches("(a*)|m|n", "man") // false
Pattern.matches("(a*)|m|n", "mn") // false

inside the [] the " " is not a quantifier so you'll get a true if one of the characters in the regex is present therefore the result will be true if the string is "a"," ","m" or "n". 在[]内,“ ”不是量词,因此如果存在正则表达式中的字符之一,则将得到true,因此如果字符串为“ a”,“, ”,“ m”或“ n“。 And the rest will result in false. 其余的将导致错误。 your regex should be: 您的正则表达式应为:

([aa*]*|[mn]) ([aa *] * | [mn])

it will be true only if multiples of "a" are entered including "a*" or a single "m" or "n". 仅当输入多个“ a”(包括“ a *”或单个“ m”或“ n”)时,它才会为真。 check it by following examples: 通过以下示例进行检查:

System.out.println("[aa*]*|[mn]","m");
System.out.println("[aa*]*|[mn]","aaaaa");
System.out.println("[aa*]*|[mn]","a*a*");

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

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