1、写一个特殊的字符串——正则表达式如a|f。

2、将正则表达式编译成一个模板:p

3、用模板p去匹配字符串str。

import java.util.regex.*; 


class Regex1{ 

 public static void main(String args[]) { 

  String str="For my money, the important thing "+"about the meeting was bridge-building"; 

  String regEx="a|f"; //表示a或f 

  Pattern p=Pattern.compile(regEx); 

  Matcher m=p.matcher(str); 

  boolean result=m.find(); 

  System.out.println(result); 

 } 

}



如果str匹配regEx,那么result为true,否则为flase。如果想在查找时忽略大小写,则可以写成:

Pattern p=Pattern.compile(regEx,Pattern.CASE_INSENSITIVE);