Skip to content

Commit

Permalink
add apoc.text.split function that splits a string using a regexp (fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
capgeti authored and jexp committed Aug 2, 2017
1 parent 1ebee7b commit 4afaead
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/text.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -20,6 +22,12 @@ result> [["<link xxx1>yyy1</link>", "xxx1", "yyy1"], ["<link xxx2>yyy2</link>",



.will split with the given regular expression return ['Hello', 'World']
[source,cypher]
----
CALL apoc.text.split('Hello World', ' +')
----

.will return 'Hello World'
[source,cypher]
----
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/apoc/text/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.stream.Stream;

import org.apache.commons.lang3.StringUtils;
import static java.util.Arrays.asList;

/**
* @author mh
Expand All @@ -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<String> 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<List<String>> regexGroups(final @Name("text") String text, final @Name("regex") String regex) {
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/apoc/text/StringsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> texts = Arrays.asList("1", "2", "3", "4");
Expand Down

0 comments on commit 4afaead

Please sign in to comment.