What regex pattern would i need to pass to String.split()
method to split a string into an array of sub strings using the white space as well as the following characters as delimiters.
(" ! ", " , " , " ? " , " . " , " \ " , " _ " , " @ " , " ' " )
and it can also be the combination of the above characters with whitespace. I've tried something like this:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
class StringWordCount {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new IputStreamReader(System.in));
String string = bufferedReader.readLine();
String delimiter = "[,\\s]+|\\[!\\s]+|\\[?\\s]+|\\[.\\s]+|\\[_\\s]+|\\[_\\s]+|\\['\\s]+|\\[@\\s]+|\\!|\\,|\\?|\\.|\\_|\\'|\\@";
String[] words = string.split(delimiter);
System.out.println(words.length);
for(int i = 0; i<words.length; i++) {
System.out.println(words[i]);
}
}
}
The above code only generates correct output for some testcases, in other cases, it won't generate the correct one.For example, Consider the below string where it failed to get the expected output.
It generates the output:
23
Hello
thanks
for
attempting
this
problem
Hope
it
will
help
you
to
learn
java
Good
luck
and
have
a
nice
day
Instead of this one:
21
Hello
thanks
for
attempting
this
problem
Hope
it
will
help
you
to
learn
java
Good
luck
and
have
a
nice
day
As you can see in the first output, its leaving a space on the combination of " ! "
and [space]
and the delimiter for the above combination is \\[!\\s]
, right?