I am learning about regular expressions and more specifically word boundries. I have a piece of code that I feel should return at least one match but it however doesn’t.
What is wrong with the code I used
public static void main(String[] args)
{
boolean matches;
String [] various = {"Men of honour", "X Men", "Children of men", "Company men are great"};
for(int i = 0; i < various.length; i++)
{
matches = Pattern.matches("\\bMen", various[i]);
System.out.println("Does the string match the pattern? " + matches);
}
}
out put is as follow
Does the string match the pattern? false
Does the string match the pattern? false
Does the string match the pattern? false
Does the string match the pattern? false
When you use
.matches(), you’re asking the regex engine if your pattern matches the whole input string. However, you want to know if your pattern can be found somewhere inside the input string.Use :