String.matchAll() should work like String.match()

This commit is contained in:
Reinhard Pointner 2016-04-03 03:30:29 +00:00
parent 6a5c604156
commit d76b74b13f
1 changed files with 3 additions and 3 deletions

View File

@ -72,7 +72,7 @@ public class ExpressionFormatMethods {
public static String match(String self, String pattern, int matchGroup) throws Exception { public static String match(String self, String pattern, int matchGroup) throws Exception {
Matcher matcher = compile(pattern, CASE_INSENSITIVE | UNICODE_CHARACTER_CLASS | MULTILINE).matcher(self); Matcher matcher = compile(pattern, CASE_INSENSITIVE | UNICODE_CHARACTER_CLASS | MULTILINE).matcher(self);
if (matcher.find()) { if (matcher.find()) {
return (matcher.groupCount() > 0 && matchGroup < 0 ? matcher.group(1) : matcher.group(matchGroup < 0 ? 0 : matchGroup)).trim(); return matcher.group(matchGroup < 0 ? matcher.groupCount() > 0 ? 1 : 0 : matchGroup).trim();
} else { } else {
throw new Exception("Pattern not found"); throw new Exception("Pattern not found");
} }
@ -82,14 +82,14 @@ public class ExpressionFormatMethods {
* Return a list of all matching patterns or break. * Return a list of all matching patterns or break.
*/ */
public static List<String> matchAll(String self, String pattern) throws Exception { public static List<String> matchAll(String self, String pattern) throws Exception {
return matchAll(self, pattern, 0); return matchAll(self, pattern, -1);
} }
public static List<String> matchAll(String self, String pattern, int matchGroup) throws Exception { public static List<String> matchAll(String self, String pattern, int matchGroup) throws Exception {
List<String> matches = new ArrayList<String>(); List<String> matches = new ArrayList<String>();
Matcher matcher = compile(pattern, CASE_INSENSITIVE | UNICODE_CHARACTER_CLASS | MULTILINE).matcher(self); Matcher matcher = compile(pattern, CASE_INSENSITIVE | UNICODE_CHARACTER_CLASS | MULTILINE).matcher(self);
while (matcher.find()) { while (matcher.find()) {
matches.add(matcher.group(matchGroup).trim()); matches.add(matcher.group(matchGroup < 0 ? matcher.groupCount() > 0 ? 1 : 0 : matchGroup).trim());
} }
if (matches.size() > 0) { if (matches.size() > 0) {