BAEL-4878 | How to split a string, but also keep the delimiters? | moved src

This commit is contained in:
root 2021-05-09 20:25:34 +00:00
parent 656d3a9ad7
commit 10b8125b09
1 changed files with 13 additions and 7 deletions

View File

@ -14,11 +14,11 @@ public class SplitAndKeepDelimitersUnitTest {
private final String positivelookAheadRegex = "((?=@))";
private final String positivelookBehindRegex = "((?<=@))";
private final String positivelookAroundRegex = "((?=@)|(?<=@))";
private final String positiveLookAroundMultiDelimiterRegex = "((?<=;|:|,|#|@|~)|(?=;|:|,|#|@|~))";
private final String positiveLookAroundMultiDelimiterRegex = "((?=:|#|@)|(?<=:|#|@))";
private String text = "Hello@World@This@Is@A@Java@Program";
private String textMixed = "@Hello;World@This:Is,A#Java~Program";
private String textMixed2 = "pg-no.10@hello;world@this:is,a#10words|Java~Program";
private String textMixed = "@HelloWorld@This:Is@A#Java#Program";
private String textMixed2 = "pg@no;10@hello;world@this;is@a#10words;Java#Program";
@Test
public void givenString_splitAndKeepDelimiters_using_javaLangString() {
@ -29,25 +29,31 @@ public class SplitAndKeepDelimitersUnitTest {
assertThat(text.split(positivelookAroundRegex)).containsExactly("Hello", "@", "World", "@", "This", "@", "Is", "@", "A", "@", "Java", "@", "Program");
assertThat(textMixed.split(positiveLookAroundMultiDelimiterRegex)).containsExactly("@", "Hello", ";", "World", "@", "This", ":", "Is", ",", "A", "#", "Java", "~", "Program");
assertThat(textMixed.split(positiveLookAroundMultiDelimiterRegex)).containsExactly("@", "HelloWorld", "@", "This", ":", "Is", "@", "A", "#", "Java", "#", "Program");
}
@Test
public void givenString_splitAndKeepDelimiters_using_ApacheCommonsLang3StringUtils() {
assertThat(StringUtils.splitByCharacterType(textMixed2)).containsExactly("pg", "-", "no", ".", "10", "@", "hello", ";", "world", "@", "this", ":", "is", ",", "a", "#", "10", "words", "|", "J", "ava", "~", "P", "rogram");
assertThat(StringUtils.splitByCharacterType(textMixed2)).containsExactly("pg", "@", "no", ";", "10", "@", "hello", ";", "world", "@", "this", ";", "is", "@", "a", "#", "10", "words", ";", "J", "ava", "#", "P", "rogram");
}
@Test
public void givenString_splitAndKeepDelimiters_using_GuavaSplitter() {
assertThat(Splitter.onPattern(positivelookAroundRegex)
.splitToList(text)).containsExactly("Hello", "@", "World", "@", "This", "@", "Is", "@", "A", "@", "Java", "@", "Program");
assertThat(Splitter.on(Pattern.compile(positivelookAroundRegex))
.splitToList(text)).containsExactly("Hello", "@", "World", "@", "This", "@", "Is", "@", "A", "@", "Java", "@", "Program");
assertThat(Splitter.onPattern(positiveLookAroundMultiDelimiterRegex)
.splitToList(textMixed)).containsExactly("@", "Hello", ";", "World", "@", "This", ":", "Is", ",", "A", "#", "Java", "~", "Program");
.splitToList(textMixed)).containsExactly("@", "HelloWorld", "@", "This", ":", "Is", "@", "A", "#", "Java", "#", "Program");
assertThat(Splitter.on(Pattern.compile(positiveLookAroundMultiDelimiterRegex))
.splitToList(textMixed)).containsExactly("@", "Hello", ";", "World", "@", "This", ":", "Is", ",", "A", "#", "Java", "~", "Program");
.splitToList(textMixed)).containsExactly("@", "HelloWorld", "@", "This", ":", "Is", "@", "A", "#", "Java", "#", "Program");
}
}