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);
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);
本文介绍如何使用正则表达式进行字符串匹配,并通过Java实现案例演示匹配过程及忽略大小写的方法。
2111

被折叠的 条评论
为什么被折叠?



