Matcher region(int start, int end) method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 07:07:01

127 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 region() method of this (Matcher) class accepts two integer values representing positions in the input string and, sets a region of the current matcher.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegionExample {    public static void main(String[] args) {       //Regular expression to accepts 6 to 10 characters       String regex = "\A(?=\w{6, 10}\z)";       System.out.println("Enter 5 to ... Read More

What is difference between matches() and find() in Java Regex?

Maruthi Krishna
Updated on 20-Nov-2019 07:04:45

313 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.Both matches() and find() methods of the Matcher class tries to find the match according to the regular expression in the input string. In case of a match, both returns true and if a match is not found both methods return false.The main difference is that the matches() method try to match the entire region of the given input i.e. if you are trying to search for ... Read More

Matcher appendReplacement() method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 07:01:17

421 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 appendReplacement() method of this (Matcher) class accepts a StringBuffer object and a String (replacement string) as parameters and, appends the input data to the StringBuffer object, replacing the matched content with the replacement string.Internally, this method reads each character from the input string and appends the String buffer, whenever a match occurs it appends the replacement string instead of matched content part of the string to ... Read More

Matcher groupCount() method in Java with Examples

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

376 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 groupCount() method of this (Matcher) class calculates the number of capturing groups in the current match.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class GroupTest {    public static void main(String[] args) {       String regex = "(.*)(\d+)(.*)";       String input = "This is a sample Text, 1234, with numbers in between.";       //Creating a pattern object       Pattern pattern ... Read More

Matcher appendTail() method in Java with Examples

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

182 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 appendTail() method of this (Matcher) class accepts a StringBuffer object and append the characters of the input sequence to it.Exampleimport java.util.regex.Matcher; import java.util.regex.Pattern; public class AppendTail {    public static void main(String[] args) {       String str = "This is an example HTML script.";       //Regular expression to match contents of the bold tags       String regex = "(\S+)";   ... Read More

Matcher toString() method in Java with Examples

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

140 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

394 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

99 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

152 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

Advertisements