Regex pattern matching in Java

Regex pattern matching in Java

Regex or Regular expressions defines a pattern for searching strings. A regular expression is a sequence of characters that helps you to find other strings using a specialised syntax held in a pattern. Regex can be a simple character, a fixed string or a complex expression containing special characters describing the pattern. The pattern defined by the regex may match one or several times or not at all for a given string. They can be used to search, edit, or manipulate string. In this tutorial we will discuss about Regex pattern matching in Java.

Regex pattern matching is widely used in Java projects. It is widely used for String validations like email validation, validation of input text restriction etc. Regex pattern is required when we use @Pattern annotation. The @Pattern annotation is used to annotate a String field in a bean(class). The annotated String must match the given regular expression otherwise an error message can be thrown. The String class also contains a matches() method which accepts a regex pattern.

Regex in Java

Java provides java.util.regex package for pattern matching with regular expressions. There are two main classes in the package. They are:

  • Pattern – An object of Pattern class is a compiled version of a regular expression. To create a regex pattern, you have to use the compile() defined in the Pattern class. You have to pass the “expression to be compiled” or in other words the “validation expression” as an argument to the compile method.
  • Matcher – A Macther object is the one that performs the actual pattern matching against an input string. You can obtain an instance of Matcher class by invoking the matcher() method on a Pattern object. You have to pass the “input string to be validated” as an argument to the matcher method.

Let us see a sample program showing usage of Pattern and Matcher class.

Below is the output of above program.

In the above program, the first pattern has been defined to accept any alphabet from (a,b,c,d,e) and any number from (1,2,3,4,5). The second pattern accepts either arun or vijay (note the | symbol).

Below are some of the pattern matching syntax

Character classes
[abc] => matches characters a, b, or c
[^abc] => matches any character except a, b, or c (negation)
.  => matches any character (may or may not match line terminators)
\d => matches a digit. It is equivalent to writing [0-9] \D => matches a non-digit. Equivalent to [^0-9] \s => matches a whitespace character: [ \t\n\x0B\f\r] \S => matches a non-whitespace character: [^\s] \w => matches a word character: [a-zA-Z_0-9] \W => matches a non-word character: [^\w] [a-zA-Z] => defines a range. a through z or A through Z, inclusive.

Boundary matchers
^ => The beginning of a line
$ => The end of a line
\b => A word boundary
\B => A non-word boundary
\A => The beginning of the input
\G => The end of the previous match
\Z => The end of the input but for the final terminator, if any
\z => The end of the input

Regex Quantifiers
X? => matches X, once or not at all
X* => matches X, zero or more times
X+ => matches X, one or more times
X{n} => matches X, exactly n times
X{n,} => matches X, at least n times
X{n,m} => matches X, at least n but not more than m times

Let us see some example Java programs with some of the syntax mentioned above.

 

 

Below is the output of running the above program.

Hope you learnt how to use Regex pattern matching in Java. Much more complex patterns can be defined using regex.

Additional Resources:

Lesson: Regular Expressions

Regular expressions simplify pattern-matching code

Java Regex – Tutorial

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

Add Comment

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