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());热门推荐
6 祝福语简短的微信
10 祝福语送超市同事简短
11 客户新房祝福语简短大气
12 学校校长新年祝福语简短
13 回归岗位文案祝福语简短
14 朋友生闺女简短祝福语
15 广场周年庆祝福语 简短
16 猪年祝福语大全简短的
17 女儿生日祝福语英语简短
18 祝福语15字以内简短