Home > Enterprise >  String.matches() returning false
String.matches() returning false

Time:11-29

if we are saying .matches() tries to match entire input string then why following returns false?

String input = "HOLIDAY"; String pattern = "H*I*Y";

input.matches(pattern) --> returns false;

Note: I have already looked at Regex doesn't work in String.matches()

CodePudding user response:

Regex is not globbing!

Your regex "H*I*Y" does not mean "H then anything then I then anything then Y"; it means "any number of H (including none) followed by any number of I (including none) followed by a Y".

The regex equivalent of globbing's * is .*: the dot means "any character" an * means "any number of (including none)".

Try:

String pattern = "H.*I.*Y";
  • Related