Can java.util.regex.Pattern do partial matches?

后端 未结 5 1659
时光取名叫无心
时光取名叫无心 2020-12-01 18:58

Is it possible to know if a stream/string contains an input that could match a regular expression.

For example

 String input=\"AA\";         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 19:44

    An alternative to hitEnd is to specify the requirement in the RE itself.

    // Accepts up to 5 'A's or 5 'A's and a 'B' (and anything following)
    Pattern pat = Pattern.compile("^(?:A{1,5}$|A{5}B)");
    boolean yes = pat.matcher("AA").find();
    boolean no = pat.matcher("BB").find();
    

提交回复
热议问题