Getting Captured: Using Regex Capturing Groups

In the previous section, we saw how to use a regular expression to search within a document to retrieve all the URLs inside it. We were able to retrieve the matching URL Strings using the find, start, and end methods of the Matcher class. Sometimes it's necessary to further process the results of a matching substring, perhaps looking for an additional subpattern. For example, you might decide not to process URLs from particular domains. The brute force approach is to use another Pattern and Matcher object for this purpose, by writing code something like this:

 // assume urlMatcher instance as in ...

Get Wicked Cool Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.