diff --git a/docs/text.adoc b/docs/text.adoc
index 481e4ae7b3..7c846e9f78 100644
--- a/docs/text.adoc
+++ b/docs/text.adoc
@@ -2,6 +2,8 @@
Basic utility procedures for string manipulation, comparison, filtering etc.
+The `replace`, `split` and `regexpGroups` functions work with regular expressions.
+
The clean functionality can be useful for cleaning up slightly dirty text data with inconsistent formatting for non-exact comparisons.
Cleaning will strip the string of all non-alphanumeric characters (including spaces) and convert it to lower case.
@@ -20,6 +22,12 @@ result> [["yyy1", "xxx1", "yyy1"], ["yyy2",
+.will split with the given regular expression return ['Hello', 'World']
+[source,cypher]
+----
+CALL apoc.text.split('Hello World', ' +')
+----
+
.will return 'Hello World'
[source,cypher]
----
diff --git a/src/main/java/apoc/text/Strings.java b/src/main/java/apoc/text/Strings.java
index 3dbc9618c6..4d9096e567 100644
--- a/src/main/java/apoc/text/Strings.java
+++ b/src/main/java/apoc/text/Strings.java
@@ -16,6 +16,7 @@
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
+import static java.util.Arrays.asList;
/**
* @author mh
@@ -37,6 +38,16 @@ public String regreplace(final @Name("text") String text, final @Name("regex") S
return text.replaceAll(regex, replacement);
}
+ @UserFunction
+ @Description("apoc.text.split(text, regex, limit) - splits the given text around matches of the given regex.")
+ public List split(final @Name("text") String text, final @Name("regex") String regex, final @Name(value = "limit", defaultValue = "0") Long limit) {
+ if (text == null || regex == null || limit == null) {
+ return null;
+ }
+ String[] resultArray = text.split(regex, limit.intValue());
+ return new ArrayList<>(asList(resultArray));
+ }
+
@UserFunction
@Description("apoc.text.regexGroups(text, regex) - return all matching groups of the regex on the given text.")
public List> regexGroups(final @Name("text") String text, final @Name("regex") String regex) {
diff --git a/src/test/java/apoc/text/StringsTest.java b/src/test/java/apoc/text/StringsTest.java
index 5a7b86ad61..535ad20f3a 100644
--- a/src/test/java/apoc/text/StringsTest.java
+++ b/src/test/java/apoc/text/StringsTest.java
@@ -10,6 +10,7 @@
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
import static apoc.util.MapUtil.map;
@@ -73,6 +74,48 @@ public void testReplaceAllWithNull() throws Exception {
row -> assertEquals(null, row.get("value")));
}
+ @Test
+ public void testSplit() throws Exception {
+ String text = "1, 2, 3,4";
+ String regex = ", *";
+
+ testCall(db,
+ "RETURN apoc.text.split({text}, {regex}) AS value",
+ map("text", text, "regex", regex),
+ row -> assertEquals(Arrays.asList("1", "2", "3", "4"), row.get("value")));
+
+ testCall(db,
+ "RETURN apoc.text.split({text}, {regex}, 2) AS value",
+ map("text", text, "regex", regex),
+ row -> assertEquals(Arrays.asList("1", "2, 3,4"), row.get("value")));
+ }
+
+ @Test
+ public void testSplitWithNull() throws Exception {
+ String text = "Hello World";
+ String regex = " ";
+
+ testCall(db,
+ "RETURN apoc.text.split({text}, {regex}) AS value",
+ map("text", null, "regex", regex),
+ row -> assertEquals(null, row.get("value")));
+
+ testCall(db,
+ "RETURN apoc.text.split({text}, {regex}) AS value",
+ map("text", text, "regex", null),
+ row -> assertEquals(null, row.get("value")));
+
+ testCall(db,
+ "RETURN apoc.text.split({text}, {regex}, null) AS value",
+ map("text", text, "regex", regex),
+ row -> assertEquals(null, row.get("value")));
+
+ testCall(db,
+ "RETURN apoc.text.split({text}, {regex}) AS value",
+ map("text", "", "regex", ""),
+ row -> assertEquals(Collections.singletonList(""), row.get("value")));
+ }
+
@Test
public void testJoin() throws Exception {
List texts = Arrays.asList("1", "2", "3", "4");