如何使用拆分或匹配器将句子拆分为单词和标点符号? [英] How to split sentence to words and punctuation using split or matcher?

查看:63
本文介绍了如何使用拆分或匹配器将句子拆分为单词和标点符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将句子拆分为单词和标点符号,并将它们放入列表中,保存它们的序列.

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

例如: 这里有一些文字!".结果应该是:List(Some, ,text, , here,!)

我正在使用 String.split("regex"); 使用split",我可以仅按单词或仅按标点符号拆分文本.

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.

推荐答案

基于

结果应该是:List(Some, ,text, , here,!)

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

看起来你想在单词边界上分割split("\\b").

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

输出:

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

这篇关于如何使用拆分或匹配器将句子拆分为单词和标点符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆