Java Reference
In-Depth Information
When the simple String methods aren't sufficient, you can use the more powerful
java.util.regex package to work with regular expressions. Create a regular ex-
pression using the Pattern class. A Matcher works on a String instance using
the pattern. All Matcher operations perform their functions using Pattern and
String instances.
The following code demonstrates how to search for both ASCII and non-ASCII
text in two separate strings. See the
org.java8recipes.chapter12.recipe12_5.Recipe12_5 class for the
complete source code. The demoSimple() method finds text with any character fol-
lowed by ".at" . The demoComplex() method finds two Japanese symbols in a
string:
public void demoSimple() {
Pattern p = Pattern.compile(".at");
Matcher m = p.matcher(enText);
while(m.find()) {
System.out.printf("%s\n", m.group());
}
}
public void demoComplex() {
Pattern p = Pattern.compile("
");
Matcher m = p.matcher(jaText);
if (m.find()) {
System.out.println(m.group());
}
}
Running these two methods on the previously defined English and Japanese text
shows the following:
fat
cat
sat
mat
rat
Search WWH ::




Custom Search