Patterns and Matcher

The Java Class Library has two special classes possessing advanced features for work with regular expressions: java.util.regex.Pattern and java.util.regex.Matcher. A Matcher object provides us with many useful methods to handle regexes, while a Pattern object represents a regular expression itself.

Regexes in programs

Regular expressions have a very wide scope of application. They’re used in text editors and in implementations of programming languages, for parsing and syntax highlighting, for extraction of useful information from files and web sites. In this lesson, we would like to show you an example of a simple yet powerful program using a regular expression for string processing.


There is a type of characters called quantifiers, which defines how often another character can occur in a regex pattern. A quantifier can be written after a regular character, as well as after a special one. In general, quantifiers are one of the most essential and important features of the regex language, since they allow a single pattern to match different strings varying in length.

Regexps in Java

A regular expression is a sequence of characters that specifies a set of strings and that is used to search, edit, and manipulate text. Like most programming languages Java supports regular expressions. We’ve already learned some basics of the regex language. In this lesson, we’ll explore how it is realized in Java.


There are some sets that are used more often than the other ones: sets for digits, or alphanumeric characters, or whitespace characters (there are quite a lot of whitespace characters, we must say). To make the usage of such sets easier and quicker, there are special shorthands, which are equivalent to certain sets, but have shorter “names”.