Regular Expression "(re)" Sub-Expression in Java

Maruthi Krishna
Updated on 19-Nov-2019 09:54:10

202 Views

The subexpression/metacharacter “( )” groups the regular expressions and remembers the matched text.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main( String args[] ) {       String input = "Hello how are you welcome to Tutorialspoint";       String regex = "H(ell|ow)";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       if(matcher.find()) {          System.out.println("Match found");       } else {       ... Read More

Explain the Metacharacter "B" in Java Regular Expressions.

Maruthi Krishna
Updated on 19-Nov-2019 09:50:33

134 Views

The subexpression/metacharacter “\B” matches the non-word boundaries.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = "\Bcause";       Scanner sc = new Scanner(System.in);       System.out.println("Enter a string: ");       String input = sc.nextLine();       Pattern p = Pattern.compile(regex);       Matcher m = p.matcher(input);       int count = 0;       while(m.find()) {          count ++;       }       System.out.println("Number of matches: "+count);    } ... Read More

How to match a character from given string including case using Java regex?

Maruthi Krishna
Updated on 19-Nov-2019 09:27:27

290 Views

The java.util.regex package of java provides various classes to find particular patterns in character sequences. The pattern class of this package is a compiled representation of a regular expression.To match a specific character from the given input string −Get the input string.This compile() method of this class accepts a string value representing a regular expression and an integer value representing a flag returns a Pattern object. Compile the regular expression bypassing −The pattern matcher “[ ]” with required character in it ex: “[t]”.The flag CASE_INSENSITIVE to ignore case.The matcher() method of the Pattern class accepts an input string and returns ... Read More

Pattern compile() method in Java with Examples

Maruthi Krishna
Updated on 19-Nov-2019 09:22:46

494 Views

The pattern class of the java.regex package is a compiled representation of a regular expression.The compile() method of this class accepts a string value representing a regular expression and returns a Pattern object.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CompileExample {    public static void main( String args[] ) {       //Reading string value       Scanner sc = new Scanner(System.in);       System.out.println("Enter input string");       String input = sc.nextLine();       //Regular expression to find digits       String regex = "(\d)";       //Compiling the regular expression ... Read More

Matcher matches() method in Java with Examples

Maruthi Krishna
Updated on 19-Nov-2019 08:15:10

340 Views

The java.util.regex.Matcher class represents an engine that performs various match operations. There is no constructor for this class you can create/obtain an object of this class using the matches() method of the class java.util.regex.Pattern.The matches() method of this class matches the string with, the pattern represented by the regular expression (both given while creating this object). In the case of a match, this method returns true else it returns false. For the result of this method to be true, the entire region should have a match.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatchesExample {    public static void main(String ... Read More

Matcher find() method in Java with Example

Maruthi Krishna
Updated on 19-Nov-2019 08:13:12

612 Views

The java.util.regex.Matcher class represents an engine that performs various match operations. There is no constructor for this class you can create/obtain an object of this class using the matches() method of the class java.util.regex.Pattern.The find() method of this class tries to find the next subsequent input matching the current Matcher object, in case of the match this method returns true else it returns false.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class FindExample {    public static void main( String args[] ) {       //Reading string value       Scanner sc = new Scanner(System.in);       System.out.println("Enter ... Read More

Matcher end() method in Java with Example

Maruthi Krishna
Updated on 19-Nov-2019 08:11:54

165 Views

The java.util.regex.Matcher class represents an engine that performs various match operations. There is no constructor for this class you can create/obtain an object of this class using the matches() method of the class java.util.regex.Pattern.The end() method of the Matcher class returns the offset after the last match represented by the current object.The subexpression "[...]" matches the characters specified within the braces in the input string, In the following example using this to match the character t. Here, We have compiled the regular expression using the compile() method.Obtained the Matcher object.Invoked the matcher() method on each match.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import ... Read More

Matcher start() method in Java with Examples

Maruthi Krishna
Updated on 19-Nov-2019 08:09:44

190 Views

The java.util.regex.Matcher class represents an engine that performs various match operations. There is no constructor for this class you can create/obtain an object of this class using the matches() method of the class java.util.regex.Pattern.The start() method of the Matcher class returns the starting index of the matched character.ExampleThe subexpression "[...]" matches the characters specified within the braces in the input string, In the following example using this to match the character t. Here, We have compiled the regular expression using the compile() method.Obtained the Matcher object.Invoked the matcher() method on each match.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class StartExample ... Read More

How to match non-word boundaries using Java RegEx?

Maruthi Krishna
Updated on 19-Nov-2019 08:07:46

220 Views

You can match the non-word boundaries using the meta character “\B”.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a String");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       String regex = "\B";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       int count = 0;     ... Read More

How to match word boundaries using Java RegEx?

Maruthi Krishna
Updated on 19-Nov-2019 08:06:01

274 Views

You can match the word boundaries using the meta character “\b”.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a String");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       String regex = "\b";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       int count = 0;     ... Read More

Advertisements