Matcher toString() Method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 06:39:44

253 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 toString() method of the Matcher class returns a string value representing the contents of the current matcher object.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ToStringExample {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");       String input = sc.nextLine();       String regex = "[#%&*]";     ... Read More

Matcher reset() Method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 06:37:41

642 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 reset() method of this (Matcher) class removes all the state information and resets the character sequence to default, and append position to zero.Example1import java.util.regex.Matcher; import java.util.regex.Pattern; public class Reset {    public static void main(String[] args) {       String str = "This is an exampleHTML script where every alternative word is bold.";       //Regular expression to match contents of the bold tags ... Read More

Matcher requireEnd Method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 06:34:40

192 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.In case of a match, the requireEnd() method of this (Matcher) class verifies whether there is a chance for the match result to be false, in case of more input, if so, this method returns true else it returns false.For example, If you are trying to match the last word of an input string to you using the regex “you$” and if your 1st input line is ... Read More

Matcher replaceFirst Method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 06:31:48

240 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 replaceFirst() method of this (Matcher) class accepts a string value and, replaces the first matched subsequence in the input text with the given string value and returns the result.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ReplaceFirstExample {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");       String input ... Read More

Matcher replaceAll Method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 06:29:06

3K+ 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 replaceAll() method of this (Matcher) class accepts a string value, replaces all the matched subsequences in the input with the given string value and returns the result.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ReplaceAllExample {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");       String input = sc.nextLine(); ... Read More

Matcher Pattern Method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 06:26:02

375 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 pattern() method of this (Matcher) class fetches and returns a Pattern (object) interpreted by the current Matcher.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PatternExample {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter your date of birth (MM/DD/YYY)");       String date = sc.next();       String regex = "^(1[0-2]|0[1-9])/(3[01]|[12][0-9]|0[1-9])/[0-9]{4}$";   ... Read More

Pattern.asPredicate() Method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 06:23:35

323 Views

The Predicate interface of the java.util.function package can be used as a target for lambda expressions. The test method of this interface accepts a value ad validates it with the current value of the Predicate object. This method returns true in-case of a match, else false.The asPredicate() method of the java.util.regex.Pattern class returns a Predicate object which can match a string with the regular expression using which the current Pattern object was compiled.Example 1import java.util.Scanner; import java.util.function.Predicate; import java.util.regex.Pattern; public class AsPredicateExample {    public static void main( String args[] ) {       //Reading string value     ... Read More

Pattern.DOTALL Field in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 06:09:35

4K+ Views

The DOTALL field of the Pattern class Enables dotall mode. By default, the “.” Meta character in regular expressions matches all characters except line terminators.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class DOTALL_Example {    public static void main( String args[] ) {       String regex = ".";       String input = "this is a sample this is second line";       Pattern pattern = Pattern.compile(regex);       Matcher matcher = pattern.matcher(input);       int count =0;       while(matcher.find()) {          count++;          System.out.print(matcher.group());     ... Read More

Pattern Comments Field in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 06:07:37

617 Views

The COMMENTS field of the Pattern class allows whitespace and comments in pattern. When you use this as flag value to the compile() method, white spaces and, comments starting with # are ignored in the given pattern.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class COMMENTES_Example {    public static void main( String args[] ) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input data: ");       String input = sc.nextLine();       //Regular expression to find digits       String regex = "\d #ignore this comment";       //Compiling ... Read More

Pattern Case-Insensitive Field in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 06:05:24

2K+ Views

This CASE_INSENSITIVE field of the Pattern class matches characters irrespective of case. When you use this as flag value to the compile() method and if you search for characters using regular expressions characters of both cases will be matched.Note − By default, this flag matches only ASCII charactersExample 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CASE_INSENSITIVE_Example {    public static void main( String args[] ) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input data: ");       String input = sc.nextLine();       System.out.println("Enter required character: ");       char ... Read More

Advertisements