简体   繁体   English

Java量词“?”如何工作?

[英]How does java quantifier “?” work?

System.out.println(Pattern.matches("[amn]?", "a"));

This statement returns true. 该语句返回true。

But

System.out.println(Pattern.matches("[amn]?", "amn"));

System.out.println(Pattern.matches("[amn]?", "adef"));

These statements returns false. 这些语句返回false。

Why ? 为什么呢

My understanding about regex quantifier "?" 我对正则表达式量词“?”的理解 is this. 这是。

Regex: X? 正则表达式: X?
Description: X occurs once or not at all 描述: X发生一次或根本不发生

So the statement "[amn]?" 所以说“ [amn]”? "amn" should return true because a,m,n occurs once. “ amn”应该返回true,因为a,m,n发生一次。 And similarly in "[amn]?" 类似地在“ [amn]”中? "adef" a occurs only once and m and n do not occur at all. “ adef” a仅出现一次,而m和n根本不出现。

Where am I going wrong ? 我要去哪里错了?

The regex [amn]? 正则表达式[amn]? matches any string which consists of either a , m or n and nothing else. 匹配任何由amn组成的字符串。 Such as "a" , which fulfills this condition. "a" ,即满足此条件。

amn and adef , however, start with one of these letters, but continue so that the "once or not at all" rule is not fulfilled. 但是, amnadef以这些字母之一开头,然后继续以使“一次或根本不执行”规则无法实现。

The first returns true because a is one letter that is either a, m or n. 第一个返回true,因为a是一个字母,可以是a,m或n。

The others return false because there's not one letter, there's 3 and 4 letters. 其他的返回false,因为没有一个字母,有3和4个字母。

Even though your letter group contains 3 letters, it will only check for the existence of one of them. 即使您的字母组包含3个字母,它也只会检查其中一个字母的存在。

Function matches() matches the entire string against the regular expression, which means it will return true only if the complete string can be matched by the expression, not any sub-sequence. 函数matches()将整个字符串与正则表达式进行匹配,这意味着仅当表达式可以匹配整个字符串,而不是任何子序列时,它才会返回true Refer this documentation . 请参阅此文档

[amn]? means that either a or m or n can exists once or not at all. 表示a或m或n可以存在一次或根本不存在。 Only cases for which matches() will return true : 仅对于matches()会返回true

  1. "a" “一种”
  2. "m" “m” 个
  3. "n" “N”
  4. "" “”

All other cases will be given as false. 所有其他情况将被视为错误。

If you want to find the regular expression in some string, then use find() as shown below. 如果要在某个字符串中查找正则表达式,请使用find() ,如下所示。

    Pattern p = Pattern.compile("[amn]?");
    Matcher mat = p.matcher("");  //pass amn or adef
    boolean matches = false;
    while(mat.find()){
        matches = true;
        break;
    }
    System.out.println(matches);

[amn] is a group consisting of the characters "a", "m" and "n". [amn]是由字符“ a”,“ m”和“ n”组成的组。 [amn]? means "one of the characters from the group [amn] or no character at all". 表示“来自[amn]组的字符之一或完全没有字符”。

Pattern.matches tries to match the entire pattern against the entire input string. Pattern.matches尝试将整个模式与整个输入字符串进行匹配。

If you want the sequence of characters "amn" you could try (amn)? 如果您想要字符序列“ amn”,可以尝试(amn)? , which should mean "the sequence 'amn' or nothing". ,其含义应为“序列'amn'或全无”。

? means match current regular expression not greedy 表示匹配当前正则表达式而不是贪婪

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

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