根据 java 中的特殊字符和圆括号拆分段落

Splitting paragraph based on special characters and parantheses and brackets in java

我正在尝试根据句点逗号和括号(如 () {} 和 [] 将一小段分成句子。如何使用 Java Regex

做这样的事情

例如,如果我有一段像

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.

我的分段结果应该是

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

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 str = "...";
str = str.replaceAll(" ?[,.()]+ ?", System.getProperty("line.separator"));

如果你想要一个数组,使用这个:

    String[] strArr = str.split(" ?[,.()]+ ?");
    for(String strr : strArr)
    {
        System.out.println(strr);
    }

产量:

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 reg = "\ ?[.,()\[\]{}]+\ ?";
String[] res = str.split(reg);

试试这个正则表达式:

 \s*[.,()\[\]{}]+\s*

示例代码

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"));
        }
    }
}

输出

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