Home> Java> javaTutorial> body text

Java regular expression logical operators

WBOY
Release: 2023-08-30 09:45:06
forward
886 people have browsed it

Java regular expression logical operators

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"); } } }
Copy after login

The Chinese translation of Output 1

is:

Output 1

Enter input text: sample text Match occurred
Copy after login

Output 2

Enter input text: hello how are you Match not occurred
Copy after login

X|Y

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

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); } }
Copy after login

Output

Number of matches: 2
Copy after login

(X): Capturing Group

Capturing groups allow you to treat multiple characters as a unit. You just put these characters in a pair of brackets.

Example

Real-time demonstration

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"); } } }
Copy after login

Output

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
Copy after login

The above is the detailed content of Java regular expression logical operators. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!