XML technologies / Regular expressions / Java / The Matcher object / Methods for building scanners

(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)

In Java 1.5, the new methods hitEnd and requireEnd, designed especially for building scanners, are available.

A scanner reads a character stream sequentially and divides it into symbols (often called tokens ). For example, a compiler's scanner reads the text ›var●<●34‹ and produces the three tokens IDENTIFIER, LESS_THAN, and INTEGER.

In a scanner, these methods determine whether to interpret the results of the last matching attempt or continue searching. Generally, a return value of true means that more input must be read before a final decision can be made. For example, with user input in an interactive debugger, after reading <, the next character must be checked to see whether it is = before deciding whether to produce LESS_THAN or LESS_THAN_OR_EQUAL.

Most regular expression applications hardly need these methods; in a few situations they are indispensable. Unfortunately, the method hitEnd has a bug that makes it practically unusable in Java 1.5. Java 1.6 fixes it, and we will see a simple workaround for Java 1.5.

A detailed discussion of scanners, parsers, and similar tools is beyond this book's scope, so I limit the discussion to definitions and examples. (If you simply need a scanner, the class java.util.Scanner may be the right choice anyway.)

 

boolean hitEnd()

(This method is unreliable in Java 1.5; see the workaround below under The hitEnd bug and its workaround.) It indicates whether the regex engine tried to read beyond the current end of input during the last matching attempt, whether successful or not. This includes checking boundaries and anchors such as ˹\b˼ and ˹$˼.

If hitEnd returns true , further input might change the outcome: failure versus success, or just the match length. Conversely, false means that the final result used only the characters already read, and further input cannot change it.

Normally, after a successful match, while hitEnd returns true , more input must be awaited. But if, after failure, hitEnd returns true , this is not a final failure: more input must be read.

 

boolean requireEnd()

This method is meaningful only after a successful match. It then indicates that the end of input contributed to that match. In other words, if requireEnd returns true , further characters might change match details, but not the fact that a match exists.

Thus, in the intended use, continue reading input while requireEnd returns true , and only then evaluate the match.

Both hitEnd and requireEnd respect the region.

 

Examples using hitEnd and requireEnd

The following table illustrates hitEnd and requireEnd after a search with lookingAt . The two regular expressions are unrealistically simple, but illustrate the principles.

The regex in the upper half looks for a natural number followed by one of four comparison operators: greater than, less than, greater than or equal, or less than or equal. The lower half simply searches for the words set or setup . These examples are very simple, but illustrate the concepts.

 

Table: hitEnd and requireEnd after a search with lookingAt.

regex

Text

Match

hitEnd()

requireEnd()

1

\d+\b|[><]=?

‘1234’

‘1234’

true

true

2

\d+\b|[><]=?

‘1234●>●567’

‘1234●>●567’

false

false

3

\d+\b|[><]=?

‘>’

‘>’

true

false

4

\d+\b|[><]=?

‘>●567’

‘>●567’

false

false

5

\d+\b|[><]=?

‘>=’

‘>=’

false

false

6

\d+\b|[><]=?

‘>=●567’

‘>=●567’

false

false

7

\d+\b|[><]=?

‘oops’

no match

false

8

(set|setup)\b

‘se’

no match

true

9

(set|setup)\b

‘set’

‘set’

true

true

10

(set|setup)\b

‘setu’

no match

true

11

(set|setup)\b

‘setup’

‘setup’

true

true

12

(set|setup)\b

‘set●x=3’

‘set●x=3’

false

false

13

(set|setup)\b

‘setup●x’

‘setup●x’

false

false

14

(set|setup)\b

‘self’

no match

false

15

(set|setup)\b

‘oops’

no match

false

In row 5, for example, a match is found, yet hitEnd is still false. Although the engine evaluated the last character in the text (which belongs to the match), it did not check anything beyond it, such as another character or a word boundary.

 

The hitEnd bug and its workaround

Java 1.5 had a very specific hitEnd bug, fixed in Java 1.6 (note: also in Java 1.5 Update 9; this book generally describes Java 1.5 Update 7; see In this chapter). It occurs only when a regex contains an optional one-character component, case is ignored, and an attempt with that component fails.

For example, the bug occurs with ˹>=?˼ (as all or part of a regex) when case is ignored, because = is a single character made optional by the question mark. It also occurs with ˹O|Oh|Huch˼ (again, case-insensitively, as all or part of a regex), because the first alternative ˹O˼ is a single character and is optional because other alternatives exist.

Other examples are ˹Werte?˼ and ˹\r?\n\r?\n˼.

Working around the bug

Avoid the bug by removing its triggering conditions. Either stop ignoring case (for the whole regex or part of it), or replace the single character with something else, such as a one-character class.

With the first approach, ˹>=?˼ becomes ˹(?-i:>=?)˼, disabling case-insensitive mode for part of the regex (see Case-insensitive mode). This is harmless here, because the character = has no uppercase or lowercase version.

With the second approach, ˹O|Oh|Huch˼ becomes ˹[oO]|Oh|Huch˼, again preserving the requirement to ignore case.

  

 

 

 

German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.

O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln

back to top