@Test public void testFindAllWithGroups() { String pattern= "\\bhref\\b\\s*=\\s*(\\S*?)[\\s>]"; String text= "<A href= a.c at >t</a>"; List<Tuple3<Integer, Integer, String>> found = RegexTools.findAll(pattern, text, true); assertEquals(1, found.size()); List<Tuple3<Integer, Integer, String>> expected= ImmutableList.of( Tuple3.tuple3(9, 12, "a.c") ); assertEquals(expected, found); }
public static List<Tuple3<Integer, Integer, String>> findAll(Pattern pattern, String text, boolean matchGroups) { if (StringUtils.isEmpty(text)) return Collections.emptyList(); Matcher matcher = pattern.matcher(text); List<Tuple3<Integer, Integer, String>> r= Lists.newArrayList(); while (matcher.find()) { int groupCount = matcher.groupCount(); if (groupCount==0 || !matchGroups) { r.add(getMatch(matcher, 0)); } else { for (int i=1; i<=groupCount; i++) { r.add(getMatch(matcher, i)); } } } return r; }
RegexTools { public static List<Tuple3<Integer, Integer, String>> findAll(Pattern pattern, String text, boolean matchGroups) { if (StringUtils.isEmpty(text)) return Collections.emptyList(); Matcher matcher = pattern.matcher(text); List<Tuple3<Integer, Integer, String>> r= Lists.newArrayList(); while (matcher.find()) { int groupCount = matcher.groupCount(); if (groupCount==0 || !matchGroups) { r.add(getMatch(matcher, 0)); } else { for (int i=1; i<=groupCount; i++) { r.add(getMatch(matcher, i)); } } } return r; } }
RegexTools { public static List<Tuple3<Integer, Integer, String>> findAll(Pattern pattern, String text, boolean matchGroups) { if (StringUtils.isEmpty(text)) return Collections.emptyList(); Matcher matcher = pattern.matcher(text); List<Tuple3<Integer, Integer, String>> r= Lists.newArrayList(); while (matcher.find()) { int groupCount = matcher.groupCount(); if (groupCount==0 || !matchGroups) { r.add(getMatch(matcher, 0)); } else { for (int i=1; i<=groupCount; i++) { r.add(getMatch(matcher, i)); } } } return r; }  }
RegexTools { public static List<Tuple3<Integer, Integer, String>> findAll(Pattern pattern, String text, boolean matchGroups) { if (StringUtils.isEmpty(text)) return Collections.emptyList(); Matcher matcher = pattern.matcher(text); List<Tuple3<Integer, Integer, String>> r= Lists.newArrayList(); while (matcher.find()) { int groupCount = matcher.groupCount(); if (groupCount==0 || !matchGroups) { r.add(getMatch(matcher, 0)); } else { for (int i=1; i<=groupCount; i++) { r.add(getMatch(matcher, i)); } } } return r; }  static List<Tuple3<Integer, Integer, String>> findAll(Pattern pattern, String text, boolean matchGroups); static List<Tuple3<Integer, Integer, String>> findAll(String pattern, String text, boolean matchGroups, RegexFlagsEnum... flags); }
RegexTools { public static List<Tuple3<Integer, Integer, String>> findAll(Pattern pattern, String text, boolean matchGroups) { if (StringUtils.isEmpty(text)) return Collections.emptyList(); Matcher matcher = pattern.matcher(text); List<Tuple3<Integer, Integer, String>> r= Lists.newArrayList(); while (matcher.find()) { int groupCount = matcher.groupCount(); if (groupCount==0 || !matchGroups) { r.add(getMatch(matcher, 0)); } else { for (int i=1; i<=groupCount; i++) { r.add(getMatch(matcher, i)); } } } return r; }  static List<Tuple3<Integer, Integer, String>> findAll(Pattern pattern, String text, boolean matchGroups); static List<Tuple3<Integer, Integer, String>> findAll(String pattern, String text, boolean matchGroups, RegexFlagsEnum... flags);  }