首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java中基于特殊字符和副词及括号的段落分割

java中基于特殊字符和副词及括号的段落分割
EN

Stack Overflow用户
提问于 2015-02-23 07:39:23
回答 3查看 113关注 0票数 0

我试图将一个小段落分割成基于句号和副词的句子,比如() {}和[]。如何使用做这样的事情?

例如,如果我有一个段落,如

到目前为止,我喜欢这辆车,开车很有趣。虽然离跑车还有很远的距离,但作为一名日常车手,它工作得很好,而且踢得很好。我有一个主要的问题,这个car.Volvo已经内置加热元件到挡风玻璃(每几毫米小电线)。晚上,所有的灯都从这些电线反射出来,使灯光模糊不清。这是一个巨大的安全问题,是非常恼人的。由于这个问题,我不确定我是否会保留这辆车很长时间。虽然有一个加热的方向盘是很好的,如果你买这辆车,跳过气候包。

我分裂段落的结果应该是

到目前为止,我喜欢这辆车,开车很有趣。 作为一名每日车手,它工作得很好,而且踢得很好。 虽然离跑车还有很远的距离 我对此有一个大问题 沃尔沃在挡风玻璃上安装了加热元件。 每隔几毫米就有一条小电线 到了晚上,所有的灯都从这些电线反射出来,使灯光模糊不清。 这是一个巨大的安全问题,而且非常烦人。 仅仅因为这个问题 我不确定我是否会把这辆车保留很久。 虽然有一个加热的方向盘很好 如果你买了这辆车,就跳过气候包

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-02-23 07:47:39

你可以试试这样的方法:

代码语言:javascript
复制
String str = "...";
str = str.replaceAll(" ?[,.()]+ ?", System.getProperty("line.separator"));

如果您想要一个数组,请使用以下命令:

代码语言:javascript
复制
    String[] strArr = str.split(" ?[,.()]+ ?");
    for(String strr : strArr)
    {
        System.out.println(strr);
    }

产量:

代码语言:javascript
复制
So far I like this car and it is fun to drive
It works very well as a daily driver and has some good kick
although it is still far from a sports car
I have one major issue with this car
Volvo has built heating elements into the windshield
small wires every few millimeters
At night all lights reflect off of these wires and makes the lights blurry
It is a huge safety issue and is extremely annoying
Due to this issue alone
I am not sure if I will keep this car very long
Although it is nice to have a heated steering wheel
skip the Climate Package if you buy this car
票数 2
EN

Stack Overflow用户

发布于 2015-02-23 07:48:28

这应该是可行的:

代码语言:javascript
复制
String reg = "\\ ?[.,()\\[\\]{}]+\\ ?";
String[] res = str.split(reg);
票数 0
EN

Stack Overflow用户

发布于 2015-02-23 07:55:20

试试这个正则表达式:

代码语言:javascript
复制
 \s*[.,()\[\]{}]+\s*

样本代码

代码语言:javascript
复制
public class Main {
    public static void main(String[] args) {
        String str = "So far I like this car and it is fun to drive. It works very well as a daily driver and has some good kick, although it is still far from a sports car. I have one major issue with this car.Volvo has built heating elements into the windshield (small wires every few millimeters). At night all lights reflect off of these wires and makes the lights blurry. It is a huge safety issue and is extremely annoying. Due to this issue alone, I am not sure if I will keep this car very long. Although it is nice to have a heated steering wheel, skip the Climate Package if you buy this car.";

        String[] output = str.split("\\s*[.,()\\[\\]{}]+\\s*");

        for (String s : output) {
            System.out.println(s + System.getProperty("line.separator"));
        }
    }
}

输出

代码语言:javascript
复制
So far I like this car and it is fun to drive

It works very well as a daily driver and has some good kick

although it is still far from a sports car

I have one major issue with this car

Volvo has built heating elements into the windshield

small wires every few millimeters

At night all lights reflect off of these wires and makes the lights blurry

It is a huge safety issue and is extremely annoying

Due to this issue alone

I am not sure if I will keep this car very long

Although it is nice to have a heated steering wheel

skip the Climate Package if you buy this car
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28668920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档