Skip to Content
Java™ Phrasebook
book

Java™ Phrasebook

by Timothy Fisher
November 2006
Intermediate to advanced
224 pages
3h 29m
English
Sams
Content preview from Java™ Phrasebook

Finding All Occurrences of a Pattern

String pattern = "\\st(\\w)*o(\\w)*";
											Pattern regPat = Pattern.compile(pattern);
											String text =
											"The words are town tom ton toon house.";
											Matcher matcher = regPat.matcher(text);
											while (matcher.find()) {
											String matchedText = matcher.group();
											System.out.println("match - " + matchedText);
											}

In the previous phrases in this chapter, we found a single match of a pattern. In this phrase, we find all the occurrences of a given match pattern that occurs within a string. The pattern we use for this phrase is "\\st(\\w)*o(\\w)*". This regular expression will find any words that begin with t and contain the letter o in them. The output printed from our System.out.println()statements will be the following:

town tom ton ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Ruby Phrasebook

Ruby Phrasebook

Jason Clinton
Wicked Cool Java

Wicked Cool Java

Brian D. Eubanks
Just Java™ 2

Just Java™ 2

Peter van der Linden
What Employees Want Most in Uncertain Times

What Employees Want Most in Uncertain Times

Kristine W. Powers, Jessica B.B. Diaz

Publisher Resources

ISBN: 0768668255Purchase book