- Java Regex - Discussion
- Java Regex - Useful Resources
- Java Regex - Quick Guide
- Java Regex - Logical Operators
- Java Regex - Possessive quantifiers
- Java Regex - Reluctant quantifiers
- Java Regex - Greedy quantifiers
- Java Regex - Boundary Matchers
- Unicode Character Classes
- Java Regex - JAVA Character Classes
- POSIX Character Classes
- Predefined Character Classes
- Java Regex - Character Classes
- Java Regex - Characters
- PatternSyntaxException Class
- Java Regex - Matcher Class
- Java Regex - Pattern Class
- Java Regex - MatchResult Interface
- Java Regex - Capturing Groups
- Java Regex - Overview
- Java Regex - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Java Regex - Quick Guide
Java Regex - Overview
Java provides the java.util.regex package for pattern matching with regular expressions. Java regular expressions are very similar to the Perl programming language and very easy to learn.
A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a speciapzed syntax held in a pattern. They can be used to search, edit, or manipulate text and data.
The java.util.regex package primarily consists of the following three classes −
Pattern Class − A Pattern object is a compiled representation of a regular expression. The Pattern class provides no pubpc constructors. To create a pattern, you must first invoke one of its pubpc static compile() methods, which will then return a Pattern object. These methods accept a regular expression as the first argument.
Matcher Class − A Matcher object is the engine that interprets the pattern and performs match operations against an input string. Like the Pattern class, Matcher defines no pubpc constructors. You obtain a Matcher object by invoking the matcher() method on a Pattern object.
PatternSyntaxException − A PatternSyntaxException object is an unchecked exception that indicates a syntax error in a regular expression pattern.
Java Regex - Capturing Groups
Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d", "o", and "g".
Capturing groups are numbered by counting their opening parentheses from the left to the right. In the expression ((A)(B(C))), for example, there are four such groups −
((A)(B(C)))
(A)
(B(C))
(C)
To find out how many groups are present in the expression, call the groupCount method on a matcher object. The groupCount method returns an int showing the number of capturing groups present in the matcher s pattern.
There is also a special group, group 0, which always represents the entire expression. This group is not included in the total reported by groupCount.
Example
Following example illustrates how to find a digit string from the given alphanumeric string −
import java.util.regex.Matcher; import java.util.regex.Pattern; pubpc class RegexMatches { pubpc static void main( String args[] ) { // String to be scanned to find the pattern. String pne = "This order was placed for QT3000! OK?"; String pattern = "(.*)(\d+)(.*)"; // Create a Pattern object Pattern r = Pattern.compile(pattern); // Now create matcher object. Matcher m = r.matcher(pne); if (m.find( )) { System.out.println("Found value: " + m.group(0) ); System.out.println("Found value: " + m.group(1) ); System.out.println("Found value: " + m.group(2) ); } else { System.out.println("NO MATCH"); } } }
This will produce the following result −
Output
Found value: This order was placed for QT3000! OK? Found value: This order was placed for QT300 Found value: 0
Java Regex - MatchResult Interface
Introduction
The java.util.regex.MatchResult interface represents the result of a match operation. This interface contains query methods used to determine the results of a match against a regular expression. The match boundaries, groups and group boundaries can be seen but not modified through a MatchResult.
Interface declaration
Following is the declaration for java.util.regex.MatchResult interface −
pubpc interface MatchResult
Interface methods
Sr.No | Method & Description |
---|---|
1 | Returns the offset after the last character matched. |
2 | Returns the offset after the last character of the subsequence captured by the given group during this match. |
3 | Returns the input subsequence matched by the previous match. |
4 | Returns the input subsequence captured by the given group during the previous match operation. |
5 | Returns the number of capturing groups in this match result s pattern. |
6 | Returns the start index of the match. |
7 | Returns the start index of the subsequence captured by the given group during this match. |
Java Regex - Pattern Class
Introduction
The java.util.regex.Pattern class represents a compiled representation of a regular expression.
Class declaration
Following is the declaration for java.util.regex.Pattern class −
pubpc final class Pattern extends Object implements Seriapzable
Field
Following are the fields for java.util.regex.Duration class −
static int CANON_EQ − Enables canonical equivalence.
static int CASE_INSENSITIVE − Enables case-insensitive matching.
static int COMMENTS − Permits whitespace and comments in pattern.
static int DOTALL − Enables dotall mode.
static int LITERAL − Enables pteral parsing of the pattern.
static int MULTILINE − Enables multipne mode.
static int UNICODE_CASE − Enables Unicode-aware case folding.
static int UNICODE_CHARACTER_CLASS − Enables the Unicode version of Predefined character classes and POSIX character classes.
static int UNIX_LINES − Enables Unix pnes mode.
Class methods
Sr.No | Method & Description |
---|---|
1 | Compiles the given regular expression into a pattern. |
2 | Compiles the given regular expression into a pattern with the given flags. |
3 | Returns this pattern s match flags. |
4 | Creates a matcher that will match the given input against this pattern. |
5 | Compiles the given regular expression and attempts to match the given input against it. |
6 | Returns the regular expression from which this pattern was compiled. |
7 | Returns a pteral pattern String for the specified String. |
8 | Sppts the given input sequence around matches of this pattern. |
9 | Sppts the given input sequence around matches of this pattern. |
10 | Returns the string representation of this pattern. |
Methods inherited
This class inherits methods from the following classes −
Java.lang.Object
Java Regex - Matcher Class
Introduction
The java.util.regex.Matcher class acts as an engine that performs match operations on a character sequence by interpreting a Pattern.
Class declaration
Following is the declaration for java.util.regex.Matcher class −
pubpc final class Matcher extends Object implements MatchResult
Class methods
Sr.No | Method & Description |
---|---|
1 | Implements a non-terminal append-and-replace step. |
2 | Implements a terminal append-and-replace step. |
3 | Returns the offset after the last character matched. |
4 | Returns the offset after the last character of the subsequence captured by the given group during the previous match operation. |
5 | Attempts to find the next subsequence of the input sequence that matches the pattern. |
6 | Resets this matcher and then attempts to find the next subsequence of the input sequence that matches the pattern, starting at the specified index. |
7 | Returns the input subsequence captured by the given group during the previous match operation. |
8 | Returns the input subsequence captured by the given named-capturing group during the previous match operation. |
9 | Returns the number of capturing groups in this matcher s pattern. |
10 | Queries the anchoring of region bounds for this matcher. |
11 | Queries the transparency of region bounds for this matcher. |
12 | Returns true if the end of input was hit by the search engine in the last match operation performed by this matcher. |
13 | Attempts to match the input sequence, starting at the beginning of the region, against the pattern. |
14 | Attempts to match the entire region against the pattern. |
15 | Returns the pattern that is interpreted by this matcher. |
16 | Returns a pteral replacement String for the specified String. |
17 | Sets the pmits of this matcher s region. |
18 | Reports the end index (exclusive) of this matcher s region. |
19 | Reports the start index of this matcher s region. |
20 | Replaces every subsequence of the input sequence that matches the pattern with the given replacement string. |
21 | Replaces the first subsequence of the input sequence that matches the pattern with the given replacement string. |
22 | Returns true if more input could change a positive match into a negative one. |
23 | Resets this matcher. |
24 | Resets this matcher with a new input sequence. |
25 | Returns the start index of the previous match. |
26 | Returns the start index of the subsequence captured by the given group during the previous match operation. |
27 | Returns the match state of this matcher as a MatchResult. |
28 | Returns the string representation of this matcher. |
29 | Sets the anchoring of region bounds for this matcher. |
30 | Changes the Pattern that this Matcher uses to find matches with. |
31 | Sets the transparency of region bounds for this matcher. |
Methods inherited
This class inherits methods from the following classes −
Java.lang.Object
Java Regex - PatternSyntaxException Class
Introduction
The java.util.regex.PatternSyntaxException class represents a unchecked exception thrown to indicate a syntax error in a regular-expression pattern.
Class declaration
Following is the declaration for java.util.regex.PatternSyntaxException class −
pubpc class PatternSyntaxException extends IllegalArgumentException
Constructors
Sr.No | Method & Description |
---|---|
1 | PatternSyntaxException(String desc, String regex, int index)
Constructs a new instance of this class. |
Class methods
Sr.No | Method & Description |
---|---|
1 | String getDescription()
Retrieves the description of the error. |
2 | int getIndex()
Retrieves the error index. |
3 | String getMessage()
Returns a multi-pne string containing the description of the syntax error and its index, the erroneous regular-expression pattern, and a visual indication of the error index within the pattern. |
4 | String getPattern()
Retrieves the erroneous regular-expression pattern. |
Methods inherited
This class inherits methods from the following classes −
Java.lang.Throwable
Java.lang.Object
Example
The following example shows the usage of java.util.regex.Pattern.PatternSyntaxException class methods.
package com.tutorialspoint; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; pubpc class PatternSyntaxExceptionDemo { private static String REGEX = "["; private static String INPUT = "The dog says meow " + "All dogs say meow."; private static String REPLACE = "cat"; pubpc static void main(String[] args) { try{ Pattern pattern = Pattern.compile(REGEX); // get a matcher object Matcher matcher = pattern.matcher(INPUT); INPUT = matcher.replaceAll(REPLACE); } catch(PatternSyntaxException e){ System.out.println("PatternSyntaxException: "); System.out.println("Description: "+ e.getDescription()); System.out.println("Index: "+ e.getIndex()); System.out.println("Message: "+ e.getMessage()); System.out.println("Pattern: "+ e.getPattern()); } } }
Let us compile and run the above program, this will produce the following result −
PatternSyntaxException: Description: Unclosed character class Index: 0 Message: Unclosed character class near index 0 [ ^ Pattern: [
Java Regex - Examples Matching Characters
Following are various examples of matching characters using regular expression in java.
Sr.No | Construct & Matches |
---|---|
1 | The character x |
2 | The backslash character |
3 |