In this tutorial we will go over list of Matcher (java.util.regex.Matcher
) APIs. Sometime back I’ve written a tutorial on Java Regex which covers wide variety of samples.
Regular Expression is a search pattern for String. java.util.regex
Classes for matching character sequences against patterns specified by regular expressions in Java.
Let’s get started.
- Create class
CrunchifyPatternMatcherTutorial.java
- Create different methods to check below Matcher APIs:
- matches()
- lookingAt()
- findStartEnd()
- group()
- multipleGroups()
- replaceAll()
- Print result of each APIs.
matches()
matches()
tries to match entire string against the pattern. It returns true
if, and only if
, the entire region sequence matches this matcher’s pattern.
lookingAt()
lookingAt() functionality is exactly same as matches() except it tries to match the input sequence, starting at the beginning of the region, against the pattern. It returns true if, and only if, a prefix of the input sequence matches this matcher’s pattern.
find(), start() and end()
find()
tries to find the next subsequence of the input sequence that matches the pattern. start()
tries to returns the start index of the previous match and end()
tries to returns the end index of the previous match.
group()
group()
returns the input subsequence matched by the previous match. It’s like match between start() and end().
multipleGroups
multiple groups could be represented by “(String) (String)”.
Sample: String crunchifyPattern = "(is) (.+?) (.+?) ";
replaceAll()
replaceAll()
replaces every subsequence of the input sequence that matches the pattern with the given replacement string.
Here is a complete example:
package crunchify.com.tutorials; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author Crunchify.com * */ public class CrunchifyPatternMatcherTutorial { public static void main(String[] args) { String crunchifyData = "This is Java Regex Pattern Example. http://crunchify.com. This is the list of all Matcher Example"; System.out.println("This is sample data 'crunchifyData': " + crunchifyData); matches(crunchifyData); lookingAt(crunchifyData); findStartEnd(crunchifyData); String crunchifyData2 = "Crunchify is a Web company. Google is a Search company. Facebook is a social company."; group(crunchifyData2); multipleGroups(crunchifyData2); replaceAll(crunchifyData2); } // crunchifyMatcher.replaceAll() private static void replaceAll(String crunchifyData2) { String crunchifyPattern = "company"; Pattern pat = Pattern.compile(crunchifyPattern); Matcher crunchifyMatcher = pat.matcher(crunchifyData2); String updatedString = crunchifyMatcher.replaceAll("best company"); System.out.println("replaceAll() - updated String: " + updatedString); } private static void multipleGroups(String crunchifyData2) { String crunchifyPattern = "(is) (.+?) (.+?) "; Pattern pat = Pattern.compile(crunchifyPattern); Matcher crunchifyMatcher = pat.matcher(crunchifyData2); while (crunchifyMatcher.find()) { System.out.println("multipleGroup() result: " + crunchifyMatcher.group()); } } // crunchifyMatcher.group() private static void group(String crunchifyData2) { String crunchifyPattern = "company"; Pattern pat = Pattern.compile(crunchifyPattern); Matcher crunchifyMatcher = pat.matcher(crunchifyData2); while (crunchifyMatcher.find()) { System.out.println("group() result: " + crunchifyMatcher.group()); } } // crunchifyMatcher.find() - start() - end() private static void findStartEnd(String crunchifyData) { String crunchifyPattern = "Example"; Pattern pat = Pattern.compile(crunchifyPattern); Matcher crunchifyMatcher = pat.matcher(crunchifyData); int totalCount = 0; while (crunchifyMatcher.find()) { totalCount++; System.out.println("findStartEnd result = Iteration " + totalCount + " : " + crunchifyMatcher.start() + " - " + crunchifyMatcher.end()); } } // crunchifyMatcher.lookingAt() private static void lookingAt(String crunchifyData) { String crunchifyPattern = "This is Java"; Pattern pat = Pattern.compile(crunchifyPattern); Matcher crunchifyMatcher = pat.matcher(crunchifyData); boolean isLookingAt = crunchifyMatcher.lookingAt(); System.out.println("lookingAt() result 1: " + isLookingAt); crunchifyPattern = " is Java"; pat = Pattern.compile(crunchifyPattern); crunchifyMatcher = pat.matcher(crunchifyData); isLookingAt = crunchifyMatcher.lookingAt(); System.out.println("lookingAt() result 2: " + isLookingAt); } // crunchifyMatcher.matches() public static void matches(String crunchifyData) { String crunchifyPattern = ".*http://.*"; Pattern pat = Pattern.compile(crunchifyPattern); Matcher crunchifyMatcher = pat.matcher(crunchifyData); boolean isMatched = crunchifyMatcher.matches(); System.out.println("matches() result: " + isMatched); } }
Result:
This is sample data 'crunchifyData': This is Java Regex Pattern Example. http://crunchify.com. This is the list of all Matcher Example matches() result: true lookingAt() result 1: true lookingAt() result 2: false findStartEnd result = Iteration 1 : 27 - 34 findStartEnd result = Iteration 2 : 90 - 97 group() result: company group() result: company group() result: company multipleGroup() result: is a Web multipleGroup() result: is a Search multipleGroup() result: is a social replaceAll() - updated String: Crunchify is a Web best company. Google is a Search best company. Facebook is a social best company.
The post How-To: Java Regex – Matcher (java.util.regex.Matcher) Tutorial appeared first on Crunchify.
Author: Arpit Shah