Java 正則表示式 - 字元類 [abc] 匹配



說明

字元類 [abc] 與 a、b 或 c 匹配。

示例

以下示例顯示了字元類匹配的使用。

package com.tutorialspoint;

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

public class CharacterClassDemo {
   private static final String REGEX = "[abc]";
   private static final String INPUT = "dbcabcabc";

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

      while(matcher.find()) {
         //Prints the start index of the match.
         System.out.println("Match String start(): "+matcher.start());
      }
   }
}

讓我們編譯並執行以上程式,它將產生以下結果 −

Match String start(): 4
Match String start(): 5
Match String start(): 6
Match String start(): 7
Match String start(): 8
javaregex_character_classes.htm
廣告
© . All rights reserved.