Java 正則表示式 - 邏輯運算子 [XY] 匹配



說明

邏輯運算子 [XY] 匹配 X 後接 Y。

示例

以下示例顯示了邏輯運算子的用法。

package com.tutorialspoint;

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

public class LogicalOperatorDemo {
   private static final String REGEX = "to";
   private static final String INPUT = "Welcome to TutorialsPoint.COM";

   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(): 8
Match String start(): 13
javaregex_logical_operators.htm
廣告
© . All rights reserved.