Reputation: 4470
I have a string There is a boy's puppy. Really?
. I need to find external puncutation and split them out from the attached word and treat them as another word. The output would be:
boy's
would be one word (internal punctuation)puppy.
would be two words, puppy
and .
Really?
would be two words, Really
and ?
The code I have splits the words on the basis of external punctuation, but I want them as the separate word.
String[] Res = word.split("[\\p{Punct}\\s]+");
How can I do that?
Upvotes: 1
Views: 145
Reputation: 51993
What you want to do with your reg ex is using a non-capturing group so that it becomes part of the output, so in the reg ex I have two groups separated by an OR (|
) where the first is capturing and the second one is non-capturing. I am not sure I've included all external punctuation you wanted in my non-capturing group, (?=X)
.
String word = "There is a boy's puppy. Really?";
String[] res = word.split("(\\s+)|(?=[\\.\\?])");
for (String s: res ) {
System.out.print("[" + s + "]");
}
Output is
[There][is][a][boy's][puppy][.][Really][?]
Upvotes: 1