示例import java.util.regex.*;
class PatternMatch{
public static void main(String args[]) {
String content = "I am a student";
String string = ".*boy.*";
boolean isMatch = Pattern.matches(string,content);
System.out.println("The line contains 'boy'?"+ isMatch);
}
}
輸出結果the line contains 'boy'?false
火柴()
用于檢查整個文本是否與模式比對。其輸出為布爾值。如果找到比對項則傳回true,否則傳回false。這是使用Regex搜尋文本中字元串的最簡單方法之一,還有另一種方法compile(),如果您要進行CASE INSENSITIVE搜尋或要搜尋多次出現可以使用。
對于上面的示例,它将是-String content = "I am a student";
String string = ".*BoY.";
Pattern pattern = Pattern.compile(string, Pattern.CASE_INSENSITIVE);