You can specify reluctant behavior by adding the ? quantifier to the pattern.
It causes the shortest matching pattern to be obtained.
// Use the ? quantifier. import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String args[]) { // Use reluctant matching behavoir. Pattern pat = Pattern.compile("e.+?d"); Matcher mat = pat.matcher("extend cup end expand endless"); while (mat.find()) System.out.println("Match: " + mat.group()); }/* www . j a va2 s . c o m*/ }