/** * Match a string with a regular expression, and returns the match as an * array. The first index is the matching expression, and array elements * [1] and higher represent each of the groups (sequences found in parens). * <p/> * This uses multiline matching (Pattern.MULTILINE) and dotall mode * (Pattern.DOTALL) by default, so that ^ and $ match the beginning and * end of any lines found in the source, and the . operator will also * pick up newline characters. */ static public String[] match(String what, String regexp) { Pattern p = matchPattern(regexp); Matcher m = p.matcher(what); if (m.find()) { int count = m.groupCount() + 1; String[] groups = new String[count]; for (int i = 0; i < count; i++) { groups[i] = m.group(i); } return groups; } return null; }
/** * Identical to match(), except that it returns an array of all matches in * the specified String, rather than just the first. */ static public String[][] matchAll(String what, String regexp) { Pattern p = matchPattern(regexp); Matcher m = p.matcher(what); ArrayList<String[]> results = new ArrayList<String[]>(); int count = m.groupCount() + 1; while (m.find()) { String[] groups = new String[count]; for (int i = 0; i < count; i++) { groups[i] = m.group(i); } results.add(groups); } if (results.isEmpty()) { return null; } String[][] matches = new String[results.size()][count]; for (int i = 0; i < matches.length; i++) { matches[i] = (String[]) results.get(i); } return matches; }
Pattern p = matchPattern(regexp); Matcher m = p.matcher(str); if (m.find()) {
Pattern p = matchPattern(regexp); Matcher m = p.matcher(str); List<String[]> results = new ArrayList<>();