如何使用拆分或匹配器将句子拆分为单词和标点符号?
How to split sentence to words and punctuation using split or matcher?
我需要将句子拆分为单词和标点符号,并将它们放入列表中,保存它们的顺序。
例如:"Some text here!"
。结果应该是:List(Some, ,text, , here,!)
我正在使用 String.split("regex");
和 "split" 我只能按单词或标点符号拆分文本。
那么我应该用什么来同时按单词和标点符号拆分文本?
提前谢谢你。
基于
And result should be: List(Some, ,text, , here,!)
您似乎想在 word boundaries split("\b")
.
拆分
String data = "Some text here!";
for (String s : data.split("\b")){
System.out.println("'"+s+"'");
}
输出:
'Some'
' '
'text'
' '
'here'
'!'
我需要将句子拆分为单词和标点符号,并将它们放入列表中,保存它们的顺序。
例如:"Some text here!"
。结果应该是:List(Some, ,text, , here,!)
我正在使用 String.split("regex");
和 "split" 我只能按单词或标点符号拆分文本。
那么我应该用什么来同时按单词和标点符号拆分文本? 提前谢谢你。
基于
And result should be: List(
Some, ,text, , here,!)
您似乎想在 word boundaries split("\b")
.
String data = "Some text here!";
for (String s : data.split("\b")){
System.out.println("'"+s+"'");
}
输出:
'Some'
' '
'text'
' '
'here'
'!'