What are regular expression and how to use regex in java?

Reading Time: 4 minutes

Data extraction from bulk and validating the input/output is an important aspect of every programming language. One of the most popular ways for this is regular expression.

What is a Regular Expression?

A regular expression is a sequence of characters that forms a search pattern.It is  also known as regex.Regular expression is used to search required information for data by the use of search pattern to describe what you are searching.

 A regular expression can be a complicated pattern or just a simple character. 

 Regular expression pattern search technique. For example :

  •  Find,edit or delete and replace text.
  •  Define Constraints on strings for email and passwords and validations.
  •  Similarly in split strings as tokens and in antlr,etc.

In Java we can import java.util.regex package to work with regex. The package includes the following class:

  •  Pattern Class
  • Matcher Class
  • PatternSyntaxException Class

Example of Regex in Java: Finds whether pattern present in given string or not.

Regular expression to finds whether pattern present in given string or not.

OUTPUT:

pattern found in matcher.

1.Pattern Class : –

Pattern Class is a compiled version of regular expression that is used to define the pattern for a regex engine.You can create a Pattern object from a pattern class.

But the Pattern class has no public constructors.You can only create an object by calling its static compile method(). This method accepts a regular expression as a parameter and it returns a Pattern object.

Methods defined in pattern class:

  • static Pattern compile(String regular expression) : It compiles the given regular expression and returns the instance of the pattern.
  • Matcher matcher(CharSequence input) : It creates a matcher that matches the given input with the pattern.
  • static boolean matches(String regular expression, CharSequence input) : It compiles the regular expression and matches the given input with the pattern.
  • String[] split(CharSequence input): It splits the given input string around matches of the given pattern.
  • String pattern() : It returns the regex expression from which this pattern was compiled.

2.Matcher Class :-

 The Matcher class helps you to match a string against a pattern. There are no public constructor in the but you can create a Matcher object by calling the match() method of the Matcher object.

 Methods defined in matcher class:

  • boolean matches(): It is used to test whether the regex matches the pattern.
  • boolean find():It is used to search multiple occurrences of the regex in the text
  • int start():The starting index of the matched sub-sequence is the returned by end method
  • int end():The ending index of the matched sub-sequence is returned by end method.

Instances of this class are not thread safe.

3.PatternSyntaxException :-

It indicates the syntax error in a regex pattern.

Methods defined in PatternSyntaxException class:

  • String getDescription():It returns the description of the error.
  • Int getIndex():It returns the error-index.
  • String getMessage():It returns a multi-line string containing: i) the description of the syntax error and its index, ii) the incorrect regular-expression pattern,iii) a visual indication of the error-index within the pattern.
  • String getPattern():Retrieves the erroneous regex pattern.

Flags added for more confined and optimised search.

Flags in the compile() method change how the search is performed.

  • Pattern.CASE_INSENSITIVE
  • Pattern.LITERAL
  • Pattern.UNICODE_CASE

Example of validating email-ID entered by user is correct or not with the use of regex patterns:

Regular expression to validate email-ID entered by user with the use of regex pattern

OUTPUT:

Email Id is correct.

Regular Expression Patterns

The first parameter of the Pattern.compile() method is the pattern. It describes what is being searched for so we are going to separate the syntax into categories.

1.Matching Characters:-

ExpressionMatches
. Any character except newline.
\wWord character (a-z, A-Z, 0-9, _)
\WNot a word character
\sWhitespaced character.
\SNon-whitespace.
\dAny digit. Same as to [0-9].
\DNon Digits.
matching characters

2.Matching Boundaries:-

ExpressionMatches
^Beginning of a string
$End of a string
\bWord boundary
\BNon word boundaries.
matching boundaries

3.Quantifiers:-

ExpressionMatches
*0 or more occurrences of the preceding expression.
+1 or more of the previous thing.
?0 or 1 occurrence of the preceding expression.
{ n}Exactly n number of occurrences of the preceding expression.
{ n,}n or more occurrences of the preceding expression.
{ n, m}Range of numbers. Minimum of n and maximum of m
quantifiers

4.Groups:-

ExpressionMatches
[…]Any single character in brackets.
[^…]Any single character not in brackets.
a| bMatches either a or b.
(re)Groups regular expressions enclosed in brackets.
groups

Example with combination of certain regex patterns:

basic pattern use for regular expression pattern matching

OUTPUT:

True

 False

 False

 True

You can be a programmer but can’t be good if you missed regex from your console,so RegEx is the best solution to the problem at hand. In conclusion, regex is the part of every programming from compiler to high level.

For more, references:-

https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

In the same way regex is a important part for other programming also for example :-

bash: https://linuxtechlab.com/bash-scripting-learn-use-regex-basics/

perl : https://perldoc.perl.org/perlre

python: https://docs.python.org/3/library/re.html

1 thought on “What are regular expression and how to use regex in java?6 min read

  1. Hi,
    Thanks alot for simplification. Easy to understand.
    It would be a great help, if you write a similar code for India mobile number verification.

    Thanks you.

Comments are closed.