Java 中扫描仪匹配()方法,示例
原文:https://www . geesforgeks . org/scanner-match-method-in-Java-with-example/
Java . util . scanner类的 match() 方法返回该扫描仪上次扫描操作的匹配结果。
语法:
public MatchResult match()
返回值:该函数返回最后一次匹配操作的匹配结果。
异常:如果没有进行匹配,或者最后一次匹配不成功,该功能将抛出非法状态异常。
以下程序说明了上述功能:
程序 1:
// Java program to illustrate the
// match() method of Scanner class in Java
// without parameter
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
String s = "GFG Geeks!";
// create a new scanner
// with the specified String Object
Scanner scanner = new Scanner(s);
// check if next token is "GFG"
System.out.println("" + scanner.hasNext("GFG"));
// find the last match and print it
System.out.println("" + scanner.match());
// print the line
System.out.println("" + scanner.nextLine());
// close the scanner
scanner.close();
}
}
输出:
true
java.util.regex.Matcher[pattern=GFG region=0, 10 lastmatch=GFG]
GFG Geeks!
程序 2: 演示非法状态异常
// Java program to illustrate the
// match() method of Scanner class in Java
// without parameter
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
String s = "GFG Geeks!";
// create a new scanner
// with the specified String Object
Scanner scanner = new Scanner(s);
// check if next token is "gopal"
System.out.println("" + scanner.hasNext("gopal"));
// find the last match and print it
System.out.println("" + scanner.match());
// print the line
System.out.println("" + scanner.nextLine());
// close the scanner
scanner.close();
}
catch (IllegalStateException e) {
System.out.println("Exception caught is: " + e);
}
}
}
输出:
false
Exception caught is: java.lang.IllegalStateException: No match result available
参考:https://docs . Oracle . com/javase/7/docs/API/Java/util/scanner . html # match()
版权属于:月萌API www.moonapi.com,转载请注明出处