Java String matches() Method Guide

In Java, the matches method is used to check if a string matches a specified regular expression. Its usage is as follows:

String str = "Hello World";
boolean isMatch = str.matches("He.*"); // 使用正则表达式判断str是否以"He"开头
System.out.println(isMatch); // 输出结果为true

In the example above, we used the matches method to check if the string “str” starts with “He”. If the string matches the regular expression, it will return true, otherwise it will return false.

bannerAds