Reputation: 2085
I need to split a string using Java's split() method. How to write the regex pattern for delimiters that is a certain word? For example, "and"?
I got the pattern for splitting space and comma which is [,\\s]
but I want to add the word, and
so that it also becomes a delimiter.
I tried many combinations including [,\\s]|(and)
but no luck.
Upvotes: 0
Views: 774
Reputation: 48404
Not really sure without an input and desired output, but you could change your last pattern to something like: \\s(?!and|,)|\\s*,\\s*|\\s+and\\s+
.
For instance:
String toSplit = "Blah,blah, foo ,bar and blah again";
System.out.println(
Arrays.toString(
toSplit.split(
// ┌ whitespace not followed by "and" or ","
// | ┌ or
// | | ┌ 0/more whitespace, ",", 0/more whitespace
// | | | ┌ or
// | | | |┌ 1/more whitespace, "and", 1/more ws
// | | | ||
"\\s(?!and|,)|\\s*,\\s*|\\s+and\\s+"
)
)
);
Output
[Blah, blah, foo, bar, blah, again]
Upvotes: 3
Reputation: 626738
You can use an alternation operator. Here is a sample program:
String string = "My mother and I";
String[] parts = string.split("(?:[,\\s]|and)");
for (int i=0; i<parts.length; i++) {
System.out.println(parts[i]);
}
Output:
My
mother
I
Upvotes: 0
Reputation: 784998
You can try:
String[] toks = input.split( "\\s*\\band\b\\s*|[,\\s]" );
Upvotes: 1