Regular Expressions In Java
We will examine Java regex with examples in the hopes that it will help us to achieve our learning goals.
Java Regular Expression
While discussing Java Regex, regular expressions (RE) are sequences of characters that make up a pattern for searching.
It is possible to use this search pattern to describe what you are looking for when you are searching for data in text.
In regular expressions, a single character can be used, or a more complicated pattern can be created.
Text search and text replacement operations can be carried out using regular expressions.
As for Java regex, there is no built-in class for regular expressions, but we can import the java.util.regex package to use regular expressions.
These classes are included in the package:
- Pattern Class – Classifies patterns (used in search).
- Matcher Class – Used to find patterns while searching
- PatternSyntaxException Class – Reports syntax errors in patterns of regular expressions
Take a look at a sentence for instances of the word “mrexamples”:
Example: 
Example: 
Example Explanation
We are searching for the word “Java Regex” in a sentence using Java regex.
In the first step, the pattern is compiled using the Pattern.compile() method. In Java regex searches, the first parameter indicates which pattern is being looked for, and the second parameter indicates whether case-insensitivity should be used.
Specifying the second parameter is an option.
Searching for a pattern in a string is done with the matcher() method. As a result, a Matcher object is returned, containing details regarding the search.
When the pattern is found in the string, find() method returns true; otherwise, it returns false.
Flags
Compile() uses flags to change how the search is conducted. As far as Java regex is concerned, here are a few examples:
- Pattern.CASE_INSENSITIVE – When searching, letter cases will not be taken into account.
- Pattern.LITERAL – When performing a search, special characters have no special significance or meaning. They will be treated as ordinary characters.
- Pattern.UNICODE_CASE – Combine it with the CASE_INSENSITIVE flag, ignores non-English alphabets
Regular Expression Patterns
Pattern.compile() method takes a pattern as its first parameter. In Java regex, it specifies the type of search that will be carried out.
A range of characters can be found by using brackets:
Expressions | Overview |
---|---|
[xyz] | Choose one character between the alternative values in the brackets. |
[^xyz] | identifies one character that is not present between the brackets. |
[0-9] | One character between the range 0 to 9 is chosen. |
Java Expression [abc]
The Expression [abc] is use to search for searching any value from the expression bracket after traversing through the whole string:
Example: 
Java Expression [^abc]
This method is used to show if any character is not present in the sentence but given in the expression:
Example: 
Java Expression [0-9]
If the Sentence under search contains any number between 0 to 9 then the following expression verifies its existence:
Example: 
Metacharacters
Metacharacters are characters that have a special meaning associated with them:
Metacharacters | Overview |
| | Looks for a match for any pattern that is divided by |, such as car|bike|bus. |
. | Only one instance of any character is found |
^ | Identifies a match as the first character of a string, as in: ^mrexamples |
$ | searches for a match at the end of the string, as in: mrexamples$ |
d | locates a digit |
s | A white space character is searched. |
\b | locates a match at the starting of a word, such as REGEX, or at the end of a word, such as REGEX. |
uxxxx | Identifies the Unicode character represented by the hexadecimal value xxxx. |
Quantifiers
Quantifiers are used to define quantities in the following ways:
Quantifiers | Overview |
n+ | Identifies any string that has at least one n. |
n* | Checks any string that has n occurring zero or more times. |
n? | Any string with zero or one occurrence of n is matched. |
n{x} | Any text that has a sequence of X n’s is matched here. |
n{x,y} | Any string containing a series of X to Y n’s is matched. |
n{x,} | Any string that includes at least X n’s is a considered as match. |
The backslash ( ) can be used to escape other special characters in your expression when searching for one of them. As you probably know, backslashes need to be escaped themselves in Java, which is why it is necessary to escape special characters with two backslashes.
For instance, if you want to find one or more question marks, you can use the following expression: “/? ”