java.util.Scanner.match()%uA0方法返回此scanner所执行的最后扫描操作的匹配结果。此方法将抛出IllegalStateException异常如果没有匹配已执行,或者如果在最后一次匹配不成功。
声明
以下是java.util.Scanner.match()方法的声明
public MatchResult match()
参数
-
NA
返回值
此方法返回一个匹配结果的最后一次匹配操作
异常
-
IllegalStateException%uA0-- 如果没有匹配的结果
例子
下面的示例演示java.util.Scanner.match()方法的用法。
package com.yiibai import java.util.* public class ScannerDemo { public static void main(String[] args) { String s = "Hello World! 3 + 3.0 = 6 " // create a new scanner with the specified String Object Scanner scanner = new Scanner(s) // check if next token is "Hello" System.out.println("" + scanner.hasNext("Hello")) // 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=Hello region=0,25 lastmatch=Hello] Hello World! 3 + 3.0 = 6