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


Java java.util.regex.MatchResult.start()用法及代碼示例


描述

這個java.time.MatchResult.start()方法返回匹配的開始索引。

聲明

以下是聲明java.time.MatchResult.start()方法。

int start()

返回值

匹配的第一個字符的索引。

異常

  • IllegalStateException- 如果還沒有嘗試過匹配,或者之前的匹配操作失敗。

示例

下麵的例子展示了 java.time.MatchResult.start() 方法的用法。

package com.tutorialspoint;

import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MatchResultDemo {
   private static final String REGEX = "(.*)(\\d+)(.*)";
   private static final String INPUT = "This is a sample Text, 1234, with numbers in between.";

   public static void main(String[] args) {
      // create a pattern
      Pattern pattern = Pattern.compile(REGEX);
      
      // get a matcher object
      Matcher matcher = pattern.matcher(INPUT); 

      if(matcher.find()) {
         //get the MatchResult Object 
         MatchResult result = matcher.toMatchResult();

         //Prints the start index of the match.
         System.out.println("First Capturing Group - Match String start():"+result.start());
      }
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果——

First Capturing Group - Match String start():0

相關用法


注:本文由純淨天空篩選整理自 java.util.regex.MatchResult.start() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。