import java.util.regex.*;
import java.util.*;
public class FindAll {
public static void main(String[] args) {
Pattern pattern = Pattern.compile(args[0]);
String text = args[1];
List<MatchResult> results = findAll(pattern, text);
for(MatchResult r : results) {
System.out.printf("Found '%s' at (%d,%d)%n",
r.group(), r.start(), r.end());
}
}
public static List<MatchResult> findAll(Pattern pattern, CharSequence text)
{
List<MatchResult> results = new ArrayList<MatchResult>();
Matcher m = pattern.matcher(text);
while(m.find()) results.add(m.toMatchResult());
return results;
}
}
|