Skip to content

Commit 1da5395

Browse files
committed
deal gracefully with null values
1 parent 5ecf846 commit 1da5395

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

src/main/java/apoc/text/Strings.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,22 @@ public String regreplace(final @Name("text") String text, final @Name("regex") S
3838
@UserFunction
3939
@Description("apoc.text.regexGroups(text, regex) - return all matching groups of the regex on the given text.")
4040
public List<List<String>> regexGroups(final @Name("text") String text, final @Name("regex") String regex) {
41-
final Pattern pattern = Pattern.compile(regex);
42-
final Matcher matcher = pattern.matcher(text);
43-
44-
List<List<String>> result = new ArrayList<>();
45-
while (matcher.find()) {
46-
List<String> matchResult = new ArrayList<>();
47-
for (int i=0;i<=matcher.groupCount(); i++) {
48-
matchResult.add(matcher.group(i));
41+
if (text==null || regex==null) {
42+
return Collections.EMPTY_LIST;
43+
} else {
44+
final Pattern pattern = Pattern.compile(regex);
45+
final Matcher matcher = pattern.matcher(text);
46+
47+
List<List<String>> result = new ArrayList<>();
48+
while (matcher.find()) {
49+
List<String> matchResult = new ArrayList<>();
50+
for (int i=0;i<=matcher.groupCount(); i++) {
51+
matchResult.add(matcher.group(i));
52+
}
53+
result.add(matchResult);
4954
}
50-
result.add(matchResult);
55+
return result;
5156
}
52-
return result;
5357
}
5458

5559

src/test/java/apoc/text/StringsTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,11 @@ public void testRegexGroups() {
240240
});
241241
}
242242

243+
@Test
244+
public void testRegexGroupsForNPE() {
245+
// throws no exception
246+
testCall(db, "RETURN apoc.text.regexGroups(null,'<link (\\\\w+)>(\\\\w+)</link>') AS result", row -> { });
247+
testCall(db, "RETURN apoc.text.regexGroups('abc',null) AS result", row -> { });
248+
}
249+
243250
}

0 commit comments

Comments
 (0)