當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java MatchResult用法及代碼示例


java中的接口就是用來實現抽象的。它也被認為是類的藍圖。接口隻能包含抽象方法,但它可能包含也可能不包含變量。換句話說,接口隻能包含抽象方法和變量。 Java 僅通過接口支持多重繼承。因此,接口在 Java 中具有更廣泛的範圍。

本文重點介紹MatchResult 接口。

MatchResult 接口:

java中的MatchResult接口表示匹配操作的結果或結論。它包含某些查詢方法的定義,可用於確定與正則表達式的匹配結果。請注意,我們無法使用 MatchResult 接口更改組/組邊界和匹配邊界,但可以通過該接口輕鬆查看它們。該接口是在 Java 1.5 版本中引入的。

可以使用以下語法聲明該接口,

用法:

public interface MatchResult

MatchResult接口的方法

下麵詳細討論MatchResult接口的查詢方法。

1.start()方法:該方法用於返回匹配的起始索引。

用法:

int start()

返回值:

  • integer: 表示第一個匹配的字符的索引

例子:

Java


// Java program to illustrate the working of start() method
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class GFG {
    // Initializing regular expression string
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
    // Initializing input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
    // Main method
    public static void main(String[] args)
    {
        // Compiling the given regular expression string
        Pattern myPattern
            = Pattern.compile(regularExpression);
        // Applying matcher operation
        Matcher myMatcher = myPattern.matcher(input);
        if (myMatcher.find()) {
            // Retrieve the MatchResult Object
            MatchResult myResult
                = myMatcher.toMatchResult();
            // Printing the starting index of the match
            System.out.println(
                "Starting index of the match: "
                + myResult.start());
        }
    }
}
輸出
Start index of the match: 0

2. start(int group):該方法返回給定組在比賽期間捕獲的序列的起始索引。

用法:

int start(int index)  

參數:

  • index:它表示匹配器模式中捕獲組的索引。

返回值:

  • non-negative integer:它表示該組所持有的第一個字符的索引
  • -1:僅當匹配成功但組don-not匹配任何內容時

例子:

Java


// Java program to illustrate the working 
// of start(int index) method
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GFG {
    // Initializing the regular expression string
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
    // Initializing the input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
    // Main method
    public static void main(String[] args)
    {
        // Compiling the regular expression string
        Pattern myPattern
            = Pattern.compile(regularExpression);
        // Applying matcher operation
        Matcher myMatcher = myPattern.matcher(input);
        if (myMatcher.find()) {
            // Retrieve the MatchResult Object
            MatchResult myResult
                = myMatcher.toMatchResult();
            // Prints the start index of the sequence
            // that was held by the given group during the
            // match.
            System.out.println("Second Capturing Group: "
                               + myResult.start(1));
        }
    }
}
輸出
Second Capturing Group: 0

3.end():該方法返回最後一個字符匹配時的偏移量。

用法:

int end() 

返回類型:它返回最後一個匹配字符之後的偏移量。

例子:

Java


// Java program to illustrate the working of end() method
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class GFG {
    // Initializing the regular expression string
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
    // Initializing the input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
    // Main method
    public static void main(String[] args)
    {
        // Compiling the regular expression string
        Pattern myPattern
            = Pattern.compile(regularExpression);
        // Applying the matcher operation
        Matcher myMatcher = myPattern.matcher(input);
        if (myMatcher.find()) {
            // Retrieve the MatchResult object
            MatchResult myResult
                = myMatcher.toMatchResult();
            // Prints the offset following the last
            // character matched
            System.out.println("First Capturing Group: "
                               + myResult.end());
        }
    }
}
輸出
First Capturing Group: 43

4. end(int index):該方法返回最後一個字符匹配時的偏移量。

用法:

int end(int index)

參數:

  • index:它表示匹配器模式中捕獲組的索引。

返回類型:它返回最後一個匹配字符之後的偏移量。

例子:

Java


// Java program to illustrate the working
// of end(int index) method
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class GFG {
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
    public static void main(String[] args)
    {
        // Compiling regular expression string
        Pattern myPattern
            = Pattern.compile(regularExpression);
        // applying matcher operation
        Matcher myMatcher = myPattern.matcher(input);
        if (myMatcher.find()) {
            // get the MatchResult Object
            MatchResult myResult
                = myMatcher.toMatchResult();
            // Prints the offset after the last
            // character of the sequence held by
            // the given group at the time of match.
            System.out.println("Second Capturing Group: "
                               + myResult.end(1));
        }
    }
}
輸出
Second Capturing Group: 18

5. group():該方法返回與前一個匹配項匹配的輸入序列。

用法:

String group()

返回類型:

  • 一個字符串:與上一個匹配項匹配的序列。

例子:

Java


// Java program to illustrate the working of group() method
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class GFG {
    // Initializing regular expression string
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
    // Initializing input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
    // Main() method
    public static void main(String[] args)
    {
        // Compiling regular expression
        Pattern myPattern
            = Pattern.compile(regularExpression);
        // Initiating the matcher object
        Matcher myMatcher = myPattern.matcher(input);
        if (myMatcher.find()) {
            // Initiating the MatchResult object
            MatchResult myResult
                = myMatcher.toMatchResult();
            // Prints the input sequence matched by the
            // previous match.
            System.out.println("First Capturing Group"
                               + myResult.group());
        }
    }
}
輸出
First Capturing GroupHello World!, 41346, these are the numbers.

6. group(int index):該方法用於返回上一次匹配操作時指定組所持有的輸入序列。

用法:

int group(int index)

參數:

  • index: 在此匹配器模式中捕獲的組的索引。

返回類型:

  • 一個字符串:它代表上一場比賽時該組捕獲的序列
  • null:如果該組無法匹配輸入的任何部分。

例子:

Java


// Java program to illustrate the working
// of group(int index) method
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class GFG {
    // Initializing regular expression string
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
    // Initializing input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
    // Main method
    public static void main(String[] args)
    {
        // Compiling regular expression
        Pattern myPattern
            = Pattern.compile(regularExpression);
        // Instantiating matcher object
        Matcher myMatcher = myPattern.matcher(input);
        if (myMatcher.find()) {
            // Instantiating MatchResult object
            MatchResult myResult
                = myMatcher.toMatchResult();
            // Prints the input subsequence that was held
            // by the given group during the last match
            // occurred.
            System.out.println(
                "Second Capturing Group - Match String: "
                + myResult.group(1));
        }
    }
}
輸出
Second Capturing Group - Match String: Hello World!, 4134

7. groupCount():該方法指定匹配結果模式中捕獲組的數量。

用法:

int groupCount()

返回類型:

  • integer:它表示匹配器模式中捕獲組的數量。

例子:

Java


// Java program to illustrate the working
// of groupCount() method
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class GFG {
    // Regular expression
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
    // Input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
    public static void main(String[] args)
    {
        // Compiling regular expression
        Pattern myPattern
            = Pattern.compile(regularExpression);
        // Instantiating a matcher object
        Matcher myMatcher = myPattern.matcher(input);
        if (myMatcher.find()) {
            // Instantiating a MatchResult Object
            MatchResult myResult
                = myMatcher.toMatchResult();
            // Prints the number of capturing groups in this
            // match result's pattern.
            System.out.println(
                "The number of capturing groups is equal to: "
                + myResult.groupCount());
        }
    }
}
輸出
The number of capturing groups is equal to: 3


相關用法


注:本文由純淨天空篩選整理自bhuwanesh大神的英文原創作品 MatchResult Interface in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。