Java Scanner match() 方法



描述

java Scanner match() 方法返回此掃描程式執行的最後一次掃描操作的匹配結果。如果尚未執行匹配,或者上次匹配不成功,則此方法將丟擲 IllegalStateException。

宣告

以下是 java.util.Scanner.match() 方法的宣告

public MatchResult match()

引數

返回值

此方法返回上次匹配操作的匹配結果

異常

IllegalStateException - 如果沒有可用的匹配結果

在字串上獲取掃描程式的最後匹配結果示例

以下示例演示瞭如何使用 Java Scanner match() 方法獲取最後一次匹配操作的匹配結果。我們使用給定的字串建立了一個掃描程式物件。我們使用 hasNext() 方法檢查字串中是否存在字串模式“Hello”,並使用 match() 列印結果。然後我們使用 nextLine() 方法列印字串,然後使用 close() 方法關閉掃描程式。

package com.tutorialspoint;

import java.util.Scanner;

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().group());

      // print the line
      System.out.println(scanner.nextLine());

      // close the scanner
      scanner.close();
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

true
Hello
Hello World! 3 + 3.0 = 6 

在使用者輸入上獲取掃描程式的最後匹配結果示例

以下示例演示瞭如何使用 Java Scanner match() 方法獲取最後一次匹配操作的匹配結果。我們使用 System.in 類建立了一個掃描程式物件。我們使用 hasNext() 方法檢查字串中是否存在字串模式“Hello”,並使用 match() 列印結果。然後我們使用 nextLine() 方法列印字串,然後使用 close() 方法關閉掃描程式。

package com.tutorialspoint;

import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) {

      // create a new scanner with the System Input
      Scanner scanner = new Scanner(System.in);

      // check if next token is "Hello"
      System.out.println(scanner.hasNext("Hello"));

      // find the last match and print it
      System.out.println(scanner.match().group());

      // print the line
      System.out.println(scanner.nextLine());

      // close the scanner
      scanner.close();
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Hello World
true
Hello
Hello World

在屬性檔案上獲取掃描程式的最後匹配結果示例

以下示例演示瞭如何使用 Java Scanner match() 方法獲取最後一次匹配操作的匹配結果。我們使用檔案 properties.txt 建立了一個掃描程式物件。我們使用 hasNext() 方法檢查字串中是否存在字串模式“Hello”,並使用 match() 列印結果。然後我們使用 nextLine() 方法列印字串,然後使用 close() 方法關閉掃描程式。

package com.tutorialspoint;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) throws FileNotFoundException {

      // create a new scanner with a file as input
      Scanner scanner = new Scanner(new File("properties.txt"));

      // check if next token is "Hello"
      System.out.println(scanner.hasNext("Hello"));

      // find the last match and print it
      System.out.println(scanner.match().group());

      // print the line
      System.out.println(scanner.nextLine());

      // close the scanner
      scanner.close();
   }
}

假設我們在你的 CLASSPATH 中有一個名為 properties.txt 的檔案,其內容如下。此檔案將用作我們示例程式的輸入:

Hello World! 3 + 3.0 = 6

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

true
Hello
Hello World! 3 + 3.0 = 6
java_util_scanner.htm
廣告

© . All rights reserved.