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 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短