java. util. regex. Can pattern do partial matching?

Whether you can know whether the stream / string contains input that can match the regular expression

for example

String input="AA";
 Pattern pat=Pattern.compile("AAAAAB");
 Matcher matcher=pat.matcher(input);
 //<-- something here returning true ?

or

String input="BB";
 Pattern pat=Pattern.compile("AAAAAB");
 Matcher matcher=pat.matcher(input);
 //<-- something here returning false ?

thank you

Solution

Yes, Java provides a method First, you must call a standard method to apply a regular expression, such as matches () or find () If false is returned, you can use the hitend () method to determine whether some longer strings can match:

String[] inputs = { "AA","BB" };
Pattern p = Pattern.compile("AAAAAB");
Matcher m = p.matcher("");
for (String s : inputs)
{
  m.reset(s);
  System.out.printf("%s -- full match: %B; partial match: %B%n",s,m.matches(),m.hitEnd());
}

Output:

AA -- full match: FALSE; partial match: TRUE
BB -- full match: FALSE; partial match: FALSE
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>