// Greedy 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 matcher.group(); } return null; }
Source : http://www.exampledepot.com/egs/java.util.regex/Greedy.html
Greedy and Nongreedy Matching in a Regular Expression
RELATED
0 COMMENT
No comment for this article.