user1631306
user1631306

Reputation: 4470

Split string into words and punctuation but don't split on internal punctuation

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:

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

Answers (1)

Joakim Danielson
Joakim Danielson

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

Related Questions