Input and Output: Regular Expressions in Java

Regular expressions are used to specify string patterns. You can use regular expressions whenever you need to locate strings that match a particular pat­tern. For example, one of our sample programs locates all hyperlinks in an HTML file by looking for strings of the pattern <a href=”. . .”>.

Of course, when specifying a pattern, the . . . notation is not precise enough. You need to specify exactly what sequence of characters is a legal match, using a special syntax to describe a pattern.

In the following sections, we cover the regular expression syntax used by the Java API and discuss how to put regular expressions to work.

1. The Regular Expression Syntax

Let us start with a simple example. The regular expression


matches any string of the following form:

  • The first letter is a J or j.
  • The next three letters are ava.
  • The remainder of the string consists of one or more arbitrary characters.

For example, the string “javanese” matches this particular regular expression, but the string “Core Java” does not.

As you can see, you need to know a bit of syntax to understand the meaning of a regular expression. Fortunately, for most purposes, a few straightforward constructs are sufficient. [1]

  • A character class is a set of character alternatives, enclosed in brackets, such as [Jj], [0-9], [A-Za-z], or [A0-9]. Here the – denotes a range (all charac­ters whose Unicode values fall between the two bounds), and A denotes the complement (all characters except those specified).
  • To include a – inside a character class, make it the first or last item. To include a ], make it the first item. To include a A, put it anywhere but the beginning. You only need to escape [ and \.
  • There are many predefined character classes such as \d (digits) or \p{Sc} (Unicode currency symbol). See Tables 2.6 and 2.7.
  • Most characters match themselves, such as the ava characters in the preceding example.
  • The . symbol matches any character (except possibly line terminators, depending on flag settings).
  • Use \ as an escape character. For example, \. matches a period and \\ matches a backslash.
  •  ^ and $ match the beginning and end of a line, respectively.
  • If X and Y are regular expressions, then XY means “any match for X followed by a match for Y.” X | Y means “any match for X or Y.”
  • You can apply quantifiers X+ (1 or more), X* (0 or more), and X? (0 or 1) to an expression X.
  • By default, a quantifier matches the largest possible repetition that makes the overall match succeed. You can modify that behavior with suffixes ? (reluctant, or stingy, match: match the smallest repetition count) and + (possessive, or greedy, match: match the largest count even if that makes the overall match fail).

For example, the string cab matches [a-z]*ab but not [a-z]*+ab. In the first case, the expression [a-z]* only matches the character c, so that the characters ab match the remainder of the pattern. But the greedy version [a-z]*+ matches the characters cab, leaving the remainder of the pattern unmatched.

  • You can use groups to define subexpressions. Enclose the groups in ( ), for example, ([+-]?)([0-9]+). You can then ask the pattern matcher to return the match of each group or to refer back to a group with \n where n is the group number, starting with \1.

For example, here is a somewhat complex but potentially useful regular expression that describes decimal or hexadecimal integers:


Unfortunately, the regular expression syntax is not completely standardized between various programs and libraries; there is a consensus on the basic constructs but many maddening differences in the details. The Java regular expression classes use a syntax that is similar to, but not quite the same as, the one used in the Perl language. Table 2.6 shows all constructs of the Java syntax. For more information on the regular expression syntax, consult the API documentation for the Pattern class or the book Mastering Regular Expressions by Jeffrey E. F. Friedl (O’Reilly and Associates, 2006).

2. Matching a String

The simplest use for a regular expression is to test whether a particular string matches it. Here is how you program that test in Java. First, construct a Pattern object from a string containing the regular expression. Then, get a Matcher object from the pattern and call its matches method:

Pattern pattern = Pattern.compite(patternString);

Matcher matcher = pattern.matcher(input);

if (matcher.matches()) . . .

The input of the matcher is an object of any class that implements the CharSequence interface, such as a String, StringBuilder, or CharBuffer.

When compiling the pattern, you can set one or more flags, for example:

Pattern pattern = Pattern.compite(expression,


Or you can specify them inside the pattern:

String regex = “(?iU: expression)”;

Here are the flags:

  • CASE_INSENSITIVE or i: Match characters independently of the letter case. By default, this flag takes only US ASCII characters into account.
  • UNICODE_CASE or u: When used in combination with CASE_INSENSITIVE, use Unicode letter case for matching.
  • UNICODE_CHARACTER_CLASS or U: Select Unicode character classes instead of POSIX. Implies UNICODE_CASE.
  • MULTILINE or m: Make A and $ match the beginning and end of a line, not the entire input.
  • UNIX_LINES or d: Only ‘\n’ is a line terminator when matching A and $ in multiline mode.
  • DOTALL or s: Make the . symbol match all characters, including line terminators.
  • COMMENTS or x: Whitespace and comments (from # to the end of a line) are ignored.
  • LITERAL: The pattern is taken literally and must be matched exactly, except possibly for letter case.
  • CANON_EQ: Take canonical equivalence of Unicode characters into account. For example, u followed by ” (diaeresis) matches u.

The last two flags cannot be specified inside a regular expression.

If you want to match elements in a collection or stream, turn the pattern into a predicate:

Stream<String> strings = . . .;

Stream<String> result = strings.fitter(pattern.asPredicate());

The result contains all strings that match the regular expression.

If the regular expression contains groups, the Matcher object can reveal the

group boundaries. The methods

int start(int groupIndex)

int end(int groupIndex)

yield the starting index and the past-the-end index of a particular group.

You can simply extract the matched string by calling

String group(int groupIndex)

Group 0 is the entire input; the group index for the first actual group is 1.

Call the groupCount method to get the total group count. For named groups, use

the methods

int start(String groupName)

int end(String groupName)

String group(String groupName)

Nested groups are ordered by the opening parentheses. For example, given the pattern


and the input


the matcher reports the following groups

Listing 2.6 prompts for a pattern, then for strings to match. It prints out whether or not the input matches the pattern. If the input matches and the pattern contains groups, the program prints the group boundaries as parentheses, for example:

3. Finding Multiple Matches

Usually, you don’t want to match the entire input against a regular expression, but to find one or more matching substrings in the input. Use the find method of the Matcher class to find the next match. If it returns true, use the start and end methods to find the extent of the match or the group method without an argument to get the matched string.

while (matcher.find())


int start = matcher.start();

int end = matcher.end();

String match =;


In this way, you can process each match in turn. As shown in the code frag­ment, you can get the matched string as well as its position in the input string.

More elegantly, you can call the results method to get a Stream<MatchResult>. The MatchResult interface has methods group, start, and end, just like Matcher. (In fact, the Matcher class implements this interface.) Here is how you get a list of all matches:

List<String> matches = pattern.matcher(input)




If you have the data in a file, you can use the Scanner.findAll method to get a Stream<MatchResult>, without first having to read the contents into a string. You can pass a Pattern or a pattern string:

var in = new Scanner(path, StandardCharsets.UTF_8);

Stream<String> words = in.findAll(“\\pL+”)


Listing 2.7 puts this mechanism to work. It locates all hypertext references in a web page and prints them. To run the program, supply a URL on the command line, such as

java match.HrefMatch

4. Splitting along Delimiters

Sometimes, you want to break an input along matched delimiters and keep everything else. The Pattern.split method automates this task. You obtain an array of strings, with the delimiters removed:

String input = . . .;

Pattern commas = Pattern.compile(“\\s*,\\s*”);

String[] tokens = commas.split(input);

// “1, 2, 3” turns into [“1”, “2”, “3”]

If there are many tokens, you can fetch them lazily:

Stream<String> tokens = commas.splitAsStream(input);

If you don’t care about precompiling the pattern or lazy fetching, you can just use the String.split method:

String[] tokens = input.split(“\\s*,\\s*”);

If the input is in a file, use a scanner:

var in = new Scanner(path, StandardCharsets.UTF_8);


Stream<String> tokens = in.tokens();

5. Replacing Matches

The replaceAll method of the Matcher class replaces all occurrences of a regular expression with a replacement string. For example, the following instructions replace all sequences of digits with a # character:

Pattern pattern = Pattern.compile(“[0-9]+”);

Matcher matcher = pattern.matcher(input);

String output = matcher.replaceAll(“#”);

The replacement string can contain references to the groups in the pattern: $n is replaced with the nth group, and ${name} is replaced with the group that has the given name. Use \$ to include a $ character in the replacement text.

If you have a string that may contain $ and \, and you don’t want them to be interpreted as group replacements, call matcher.replaceAll(Matcher.quoteReplacement(str)).

If you want to carry out a more complex operation than splicing in group matches, you can provide a replacement function instead of a replacement string. The function accepts a MatchResult and yields a string. For example, here we replace all words with at least four letters with their uppercase version:

String result = Pattern.compile(“\\pL{4,}”)

.matcher(“Mary had a little lamb”)

.replaceAll(m ->;

// Yields “MARY had a LITTLE LAMB”

The replaceFirst method replaces only the first occurrence of the pattern.

You have now seen how to carry out input and output operations in Java, and had an overview of the regular expression package that was a part of the “new I/O” specification. In the next chapter, we turn to the processing of XML data.

Source: Horstmann Cay S. (2019), Core Java. Volume II – Advanced Features, Pearson; 11th edition.

Leave a Reply

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