XML technologies / Regular expressions / Java / The Matcher object / Querying results

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

The following Matcher methods retrieve match data after a successful search. If the regex has not been applied, or the last search failed, IllegalStateException is thrown. For methods accepting a capturing-group number, IndexOutOfBoundsException is thrown if the number is invalid.

The offsets returned by methods start and end are relative to the input string’s beginning, not the current region’s beginning.

An example illustrating these methods follows the list.

 

String group()

Returns the text matched by the last search.

 

int groupCount()

Returns the number of capturing groups in the object’s regex. Methods group, start and end accept numbers up to this limit. Method groupCount can always be called; the others require a successful search.

 

String group(int num)

Returns text captured by group number num , or null if that group did not participate. Using 0 for num retrieves the whole match; group(0) is therefore equivalent to group().

 

int start(int num)

Returns the character offset from the string’s beginning to the start of the text captured by group number num . If the group did not participate, returns -1.

 

int start()

The match’s starting offset, equivalent to start(0).

 

int end(int num)

Returns the character offset from the string’s beginning to the end of the text captured by group number num . If the group did not participate, returns -1.

 

int end()

The match’s ending offset, equivalent to end(0).

 

MatchResult toMatchResult()

Since Java 1.5, method toMatchResult returns the latest search’s match data in a MatchResult object. It offers the same query methods as class Matcher : group, start, end and groupCount.

If toMatchResult is called after a failed search or before any search, exception IllegalStateException is thrown.

 

Code example: match results

This example illustrates result queries. Given a URL string, it extracts the protocol (‘http’ or ‘https’), hostname and optional port.

String url   = "http://regex.info/blog";
String regex = "(?x) ^(https?):// ([^/:]+) (?:(\\d+))?";
Matcher m = Pattern.compile(regex).matcher(url);
if (m.find())
{
  System.out.print(
      "Komplett  [" + m.group()  + "]" +
      " (von "      + m.start()  + " bis " + m.end()  + ")\n" +
      "Protokoll [" + m.group(1) + "]" +
      " (von "      + m.start(1) + " bis " + m.end(1) + ")\n" +
      "Hostname  [" + m.group(2) + "]" +
      " (von "      + m.start(2) + " bis " + m.end(2) + ")\n"
  );
  // Vorsicht, das 3. Klammerpaar ist vielleicht nicht Teil des Treffers.
  if (m.group(3) == null)
     System.out.println("Keine Portnummer; Voreinstellung ist '80'");
  else {
     System.out.print("Portnummer [" + m.group(3) + "] " +
                      "(von " + m.start(3) + " bis " + m.end(3) + ")\n");
  }
}

The program prints:

Komplett  [http://regex.info] (von 0 bis 17) 
 Protokoll [http] (von 0 bis 4) 
 Hostname  [regex.info] (von 7 bis 17) 
 Keine Portnummer; Voreinstellung ist '80'

  

 

 

 

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