Matcher.find(int)方法在Java正则表达式中的作用
Matcher.find(int)方法在指定为参数的子序列号之后找到输入序列中的子序列。在java.util.regex包中可用的Matcher类中可以使用此方法。
Matcher.find(int)方法具有一个参数,即子序列号,此子序列号之后将获得子序列,并且返回true就是获得所需的子序列,否则返回false。
一个程序演示了Java正则表达式中的Matcher.find(int)方法,如下所示-
示例
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Demo {
public static void main(String args[]) {
Pattern p = Pattern.compile("apple", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("apple APPLE Apple aPPLE");
System.out.println("apple APPLE Apple aPPLE");
m.find(6);
System.out.println(m.group());
System.out.println("\napple APPLE Apple aPPLE");
m.find(12);
System.out.println(m.group());
System.out.println("\napple APPLE Apple aPPLE");
m.find(0);
System.out.println(m.group());
}
}上面程序的输出如下-
apple APPLE Apple aPPLE APPLE apple APPLE Apple aPPLE Apple apple APPLE Apple aPPLE apple
现在让我们了解上面的程序。
使用find(int)方法找到子序列号6、12和0之后的输入序列“appleAPPLEAppleaPPLE”中的子序列,并打印所需的结果。演示这的代码片段如下-
Pattern p = Pattern.compile("apple", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("apple APPLE Apple aPPLE");
System.out.println("apple APPLE Apple aPPLE");
m.find(6);
System.out.println(m.group());
System.out.println("\napple APPLE Apple aPPLE");
m.find(12);
System.out.println(m.group());
System.out.println("\napple APPLE Apple aPPLE");
m.find(0);
System.out.println(m.group());热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短