How to split sentence to words and punctuation usi

2019-09-24 09:08发布

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条回答
走好不送
2楼-- · 2019-09-24 09:56

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'
'!'
查看更多
登录 后发表回答