//  quantifiers
String match = find("A.*c", "AbcAbc");  // AbcAbc
match = find("A.+", "AbcAbc");          // AbcAbc

// Nongreedy quantifiers
match = find("A.*?c", "AbcAbc");        // Abc
match = find("A.+?", "AbcAbc");         // Abc

// Returns the first substring in input that matches the pattern.
// Returns null if no match found.
public static String find(String patternStr, CharSequence input) {
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(input);
    if (matcher.find()) {
    return null;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.