首頁 > Java > java教程 > 主體

Java正規表示式邏輯運算符

WBOY
發布: 2023-08-30 09:45:06
轉載
963 人瀏覽過

Java正規表示式邏輯運算符

Java Regular expressions supports 3 logical operators they are −

  • XY: X followed by Y

  • #X|Y: X or Y

  • (X): capturing group.

XY: X followed by Y

This simply matches two single consecutive characters.

Example

 Live Demo

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter input text: ");
      String input = sc.nextLine();
      String regex = "am";
      //Creating a pattern object
      Pattern pattern = Pattern.compile(regex);
      //Matching the compiled pattern in the String
      Matcher matcher = pattern.matcher(input);
      if (matcher.find()) {
         System.out.println("Match occurred");
      } else {
         System.out.println("Match not occurred");
      }
   }
}
登入後複製

Output 1

的中文翻譯為:

輸出1

#
Enter input text:
sample text
Match occurred
登入後複製

Output 2

Enter input text:
hello how are you
Match not occurred
登入後複製

X|Y

This matches either of the two expressions/characters surrounding "|"

Example

 Live Demoounding "|"

Example

## Live Demo

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
   public static void main( String args[] ) {
      String regex = "Hello|welcome";
      String input = "Hello how are you welcome to Tutorialspoint";
      Pattern pattern = Pattern.compile(regex);
      Matcher matcher = pattern.matcher(input);
      int count = 0;
      while(matcher.find()) {
         count++;
      }
      System.out.println("Number of matches: "+count);
   }
}
登入後複製

輸出

Number of matches: 2
登入後複製

(X): 擷取群組

#擷取群組可讓您將多個字元視為一個單元。您只需將這些字元放在一對括號中即可。

範例

 即時示範

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CapturingGroups {
   public static void main( String args[] ) {
      System.out.println("Enter input text");
      Scanner sc = new Scanner(System.in);
      String input = sc.nextLine();
      String regex = "(.*)(\d+)(.*)";
      //Create a Pattern object
      Pattern pattern = Pattern.compile(regex);
      //Now create matcher object.
      Matcher matcher = pattern.matcher(input);
      if (matcher.find( )) {
         System.out.println("Found value: " + matcher.group(0) );
         System.out.println("Found value: " + matcher.group(1) );
         System.out.println("Found value: " + matcher.group(2) );
         System.out.println("Found value: " + matcher.group(3) );
      } else {
         System.out.println("NO MATCH");
      }
   }
}
登入後複製
###輸出###
Enter input text
sample data with 1234 (digits) in middle
Found value: sample data with 1234 (digits) in middle
Found value: sample data with 123
Found value: 4
Found value: (digits) in middle
登入後複製
###

以上是Java正規表示式邏輯運算符的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板