Matcher类的region(int,int)方法限制该模式要匹配的区域。此区域必须小于或等于前一个区域,但不能大于。否则将导致IndexOutOfBoundsException。此方法返回具有新匹配区域的Matcher。
用法:
public Matcher region(int startIndex, int endIndex)
参数:此方法有两个参数:
- startIndex这是新禁区的起始指标。
- endIndex这是新禁区的结束 index 。
返回值:此方法返回一个Matcher,其中包含要匹配的区域。
异常:在以下情况下,此方法将引发IndexOutOfBoundsException:
- startIndex或endIndex小于零,
- startIndex或endIndex大于输入序列的长度,
- startIndex大于endIndex。
下面的示例说明Matcher.region()方法:
范例1:
// Java code to illustrate region() method
import java.util.regex.*;
public class GFG {
public static void main(String[] args)
{
// Get the regex to be checked
String regex = "(Geeks)";
// Create a pattern from regex
Pattern pattern = Pattern.compile(regex);
// Get the String to be matched
String stringToBeMatched
= "GeeksForGeeks Geeks for For Geeks Geek";
// Create a matcher for the input String
Matcher matcher
= pattern.matcher(stringToBeMatched);
System.out.println("Before changing region, "
+ " Groups found are:");
while (matcher.find()) {
// Get the group matched using group() method
System.out.println(matcher.group());
}
System.out.println("\nAfter changing region, "
+ " Groups found are:");
// Restrict the region to 0, 10
// using region() method
Matcher matcher1
= matcher.region(0, 10);
while (matcher1.find()) {
// Get the group matched using group() method
System.out.println(matcher1.group());
}
}
}
输出:
Before changing region, Groups found are: Geeks Geeks Geeks Geeks After changing region, Groups found are: Geeks
范例2:
// Java code to illustrate region() method
import java.util.regex.*;
public class GFG {
public static void main(String[] args)
{
// Get the regex to be checked
String regex = "(FGF)";
// Create a pattern from regex
Pattern pattern
= Pattern.compile(regex);
// Get the String to be matched
String stringToBeMatched
= "FGF GFG GFG FGF";
// Create a matcher for the input String
Matcher matcher
= pattern.matcher(stringToBeMatched);
System.out.println("Before changing region, "
+ " Groups found are:");
while (matcher.find()) {
// Get the group matched using group() method
System.out.println(matcher.group());
}
System.out.println("\nAfter changing region, "
+ " Groups found are:");
// Restrict the region to 0, 5
// using region() method
Matcher matcher1 = matcher.region(0, 5);
while (matcher1.find()) {
// Get the group matched using group() method
System.out.println(matcher1.group());
}
}
}
输出:
Before changing region, Groups found are: FGF FGF After changing region, Groups found are: FGF
参考: https://docs.oracle.com/javase/9/docs/api/java/util/regex/Matcher.html#region-int-int-
相关用法
- Java Matcher end()用法及代码示例
- Java Matcher end(int)用法及代码示例
- Java Matcher hasTransparentBounds()用法及代码示例
- Java Matcher groupCount()用法及代码示例
- Java Matcher toMatchResult()用法及代码示例
- Java Matcher matches()用法及代码示例
- Java Matcher find()用法及代码示例
- Java Matcher find(int)用法及代码示例
- Java Matcher group(int)用法及代码示例
- Java Matcher lookingAt()用法及代码示例
- Java Matcher group()用法及代码示例
- Java Matcher regionStart()用法及代码示例
- Java Matcher regionEnd()用法及代码示例
- Java Matcher start()用法及代码示例
- Java Matcher start(int)用法及代码示例
注:本文由纯净天空筛选整理自Kirti_Mangal大神的英文原创作品 Matcher region(int, int) method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。