Java中具有示例的Matcher region(int start,int end)方法
java.util.regex.Matcher类表示执行各种匹配操作的引擎。此类没有构造函数,可以使用类java.util.regex.Pattern的matchs()方法创建/获取此类的对象。
此类(Matcher)类的region()方法接受表示输入字符串中位置的两个整数值,并设置当前匹配器的区域。
例子1
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegionExample {
public static void main(String[] args) {
//Regular expression to accepts 6 to 10 characters
String regex = "\\A(?=\\w{6,10}\\z)";
System.out.println("Enter 5 to 12 characters: ");
String input = new Scanner(System.in).next();
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Creating a Matcher object
Matcher matcher = pattern.matcher(input);
//Setting region to the input string
matcher.region(0, 4);
//Switching to transparent bounds
if(matcher.find()) {
System.out.println("Match found");
} else {
System.out.println("Match not found");
}
}
}输出结果
Enter 5 to 12 characters: sampleText Match not found
例子2
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegionExample {
public static void main(String[] args) {
String regex = "(.*)(\\d+)(.*)";
String input = "This is a sample Text, 1234, with numbers in between.";
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Creating a Matcher object
Matcher matcher = pattern.matcher(input);
//Setting the region of the matcher
matcher.region(0, 20);
if(matcher.matches()) {
System.out.println("Match found");
} else {
System.out.println("Match not found");
}
}
}输出结果
Match not found
热门推荐
10 送灯祝福语简短独特
11 金店立秋祝福语简短英文
12 姐姐女儿结婚祝福语简短
13 宝宝周岁可乐祝福语简短
14 婆婆生日祝福语简短的
15 航空公司祝福语简短
16 虎年新年专属祝福语简短
17 蛋糕祝老师祝福语简短
18 英语简短祝福语给学生