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
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
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
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
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
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
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
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
The pattern class of 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, the following is the signature of this method.static Pattern compile(String regex)Another variant of this method accepts an integer value representing flags, following is the signature of the compile method with two parameters.static Pattern compile(String regex, int flags)The Pattern class provides various fields each representing a flagS.NoField and Description1CANON_EQMatches two characters only if they are canonically equal.2CASE_INSENSITIVEMatches characters irrespective of case.3COMMENTSAllows whitespace and comments in pattern.4DOTALLEnables dotall mode. Where ... Read More
The Pattern class of the java.util.regex package is a compiled representation of a regular expression.The toString() method of this class returns the string representation of the regular expression using which the current Pattern was compiled.Example1import java.util.Scanner; import java.util.regex.Pattern; public class Example { 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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP