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: 

import java.util.regex.Matcher; import java.util.regex.Pattern;public class Main { public static void main(String[] args) { Pattern pattern = Pattern.compile("mrexamples", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher("Visit mrexamples!"); boolean matchFound = matcher.find(); if(matchFound) { System.out.println("Match found"); } else { System.out.println("Match not found"); } } } // We found the output as Match Found

Example: 

import java.util.regex.Matcher; // Here we import the Matcher class from the java.util package import java.util.regex.Pattern; // Similarly the regex.Pattern class is called herepublic class Main { public static void main(String[] args) { Pattern pattern_match = Pattern.compile("Java regex", Pattern.CASE_INSENSITIVE); Matcher syntax_matcher = pattern_match.matcher("Hello Dear Learner !! We are learning about Java regex in this session"); boolean matchFound = syntax_matcher.find(); if(matchFound) { System.out.println("The Sentence contains the word Java regex !!"); } else { System.out.println("Java Regex not found !!"); } } } // Result will be as: The Sentence contains the word Java regex !!


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:

  1. Pattern.CASE_INSENSITIVE – When searching, letter cases will not be taken into account.
  2. Pattern.LITERAL – When performing a search, special characters have no special significance or meaning. They will be treated as ordinary characters.
  3. 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:

ExpressionsOverview
[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: 

import java.util.regex.Matcher; // Here we import the Matcher class from the java.util package import java.util.regex.Pattern; // Similarly the regex.Pattern class is called herepublic class Main { public static void main(String[] args) { Pattern pattern_match = Pattern.compile("[zmq]", Pattern.CASE_INSENSITIVE); Matcher syntax_matcher = pattern_match.matcher("Hello Dear Learner !! I hope you will be having fun learning java"); boolean matchFound = syntax_matcher.find(); if(matchFound) { System.out.println("Match Found !! The Sentence contains the characters z,m,q !!"); } else { System.out.println("Match not found!!"); } } }

Java Expression [^abc]

This method is used to show if any character is not present in the sentence but given in the expression:

Example: 

import java.util.regex.Matcher; // Here we import the Matcher class from the java.util package import java.util.regex.Pattern; // Similarly the regex.Pattern class is called herepublic class Main { public static void main(String[] args) { Pattern pattern_match = Pattern.compile("[^zmq]", Pattern.CASE_INSENSITIVE); Matcher syntax_matcher = pattern_match.matcher("Hello Dear Learner !! I hope you will be having fun learning java"); boolean matchFound = syntax_matcher.find(); if(matchFound) { System.out.println("True !! The Sentence do not contain the characters as z,m,q "); } else { System.out.println("False !! z,m,q are a part of the sentence !!"); } } }

 

Java Expression [0-9]

If the Sentence under search contains any number between 0 to 9 then the following expression verifies its existence:

Example: 

import java.util.regex.Matcher; // Here we import the Matcher class from the java.util package import java.util.regex.Pattern; // Similarly the regex.Pattern class is called herepublic class Main { public static void main(String[] args) { Pattern pattern_match = Pattern.compile("[0-9]", Pattern.CASE_INSENSITIVE); Matcher syntax_matcher = pattern_match.matcher("Hello Dear Learner 24 !! I hope you will be having fun learning java"); boolean matchFound = syntax_matcher.find(); if(matchFound) { System.out.println("True !! The Sentence contains a number between 0 to 9 "); } else { System.out.println("False !! Sentence do not contain any number from 0 to 9 !!"); } } }

 

Metacharacters

Metacharacters are characters that have a special meaning associated with them:

MetacharactersOverview
|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$
dlocates a digit
s A white space character is searched.
\blocates a match at the starting of a word, such as REGEX, or at the end of a word, such as REGEX.
uxxxxIdentifies the Unicode character represented by the hexadecimal value xxxx.

Quantifiers

Quantifiers are used to define quantities in the following ways:

QuantifiersOverview
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: “/? ”

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *