How to split sentence to words and punctuation usi

2019-09-24 09:03发布

问题:

I need to split sentence to words and punctuation marks, and place em into list, saving their sequence.

For example: "Some text here!". And result should be: List(Some, ,text, , here,!)

I'm using String.split("regex"); With "split" I can split text only by word or only by punctuation.

So what should I use, to split text by words and punctuation at same time? Thank you in advance.

回答1:

Based on

And result should be: List(Some, ,text, , here,!)

it looks like you want to split on word boundaries split("\\b").

String data = "Some text here!";
for (String s : data.split("\\b")){
    System.out.println("'"+s+"'");
}

Output:

'Some'
' '
'text'
' '
'here'
'!'