Found 2616 Articles for Java

Matcher reset() method in Java with Examples

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

437 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

111 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

160 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

259 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

223 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 UNICODE_CHARACTER_CLASS field in Java with examples

Maruthi Krishna
Updated on 06-Dec-2023 12:41:53

484 Views

Enables the Unicode version of Predefined character classes and POSIX character classes. Example import java.util.regex.Matcher; import java.util.regex.Pattern; public class UNICODE_CHARACTER_CLASS_Example { public static void main( String args[] ) { String regex = "\u00de"; //Compiling the regular expression Pattern pattern = Pattern.compile(regex, Pattern.UNICODE_CHARACTER_CLASS); //Retrieving the matcher object String str[] = {"\u00de", "\u00fe", "\u00ee", "\u00ce"}; for (String ele : str) { Matcher matcher = pattern.matcher(ele); ... Read More

Pattern UNIX_LINES field in Java with Examples

Maruthi Krishna
Updated on 07-Dec-2023 09:56:32

215 Views

This flag enables Unix lines mode. In the Unix lines mode, only '' is used as a line terminator and ‘\r’ is treated as a literal character. Example 1 import java.util.regex.Matcher; import java.util.regex.Pattern; public class LTERAL_Example { public static void main(String[] args) { String input = "This is the first line\r" + "This is the second line\r" + "This is the third line\r"; //Regular expression to accept date in MM-DD-YYY format ... Read More

Pattern UNICODE_CASE field in Java with Examples

Maruthi Krishna
Updated on 07-Dec-2023 10:03:09

788 Views

Enables Unicode-aware case folding. When you use this as flag value to the compile() method along with the CASE_INSENSITIVE flag and if you search for Unicode characters using regular expressions Unicode characters of both cases will be matched. Example import java.util.regex.Matcher; import java.util.regex.Pattern; public class UNICODE_CASE_Example { public static void main( String args[] ) { String regex = "\u00de"; //Compiling the regular expression Pattern pattern = Pattern.compile(regex, Pattern.UNICODE_CASE|Pattern.CASE_INSENSITIVE); //Retrieving the matcher object String str[] = {"\u00de", "\u00fe", ... Read More

Pattern MULTILINE field in Java with examples

Maruthi Krishna
Updated on 05-Dec-2023 10:40:19

2K+ Views

Enables multiline mode in general, the ^ and $ meta characters matches the start and end of the given input with the specified characters irrespective of the number of lines in it. Example 1 import java.util.regex.Matcher; import java.util.regex.Pattern; public class MULTILINE_Example { public static void main( String args[] ) { //String regex = "(^This)";//.*t$)"; String input = "2234 This is a sample text" + "1424 This second 2335 line" + "This id third 455 line" ... Read More

Advertisements