Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

830 Part II: The Java Library


The output from the program is shown here:

Match: W
Match: WW
Match: WWW

As the output shows, the regular expression pattern “W+” matches any arbitrarily long
sequence of Ws.
The next program uses a wildcard to create a pattern that will match any sequence that
begins witheand ends withd. To do this, it uses the dot wildcard character along with the+
quantifier.

// Use wildcard and quantifier.
import java.util.regex.*;

class RegExpr5 {
public static void main(String args[]) {
Pattern pat = Pattern.compile("e.+d");
Matcher mat = pat.matcher("extend cup end table");

while(mat.find())
System.out.println("Match: " + mat.group());
}
}

You might be surprised by the output produced by the program, which is shown here:

Match: extend cup end

Only one match is found, and it is the longest sequence that begins witheand ends withd.
You might have expected two matches: “extend” and “end”. The reason that the longer
sequence is found is that by default,find( )matches the longest sequence that fits the
pattern. This is calledgreedy behavior.You can specifyreluctant behaviorby adding the?
quantifier to the pattern, as shown in this version of the program. It causes the shortest
matching pattern to be obtained.

// Use the? quantifier.
import java.util.regex.*;

class RegExpr6 {
public static void main(String args[]) {
// Use reluctant matching behavior.
Pattern pat = Pattern.compile("e.+?d");
Matcher mat = pat.matcher("extend cup end table");

while(mat.find())
System.out.println("Match: " + mat.group());
}
}

The output from the program is shown here:

Match: extend
Match: end
Free download pdf