我试图根据句号和(){}和[]之类的句号将一小段分成句子。如何使用Java Regex做这样的事情
例如,如果我有一个类似
的段落到目前为止,我喜欢这款车,开车很有趣。虽然距离跑车还很远,但作为日常驾驶员,它的效果非常好。我对这款车有一个主要问题。沃尔沃在挡风玻璃上建造了加热元件(每隔几毫米就有一根小电线)。到了晚上,所有的灯都反射出这些电线,使灯光变得模糊。这是一个巨大的安全问题,非常烦人。由于这个问题,我不确定我是否会将这辆车保留很长时间。虽然拥有一个加热方向盘很不错,但如果您购买这款车,请跳过气候套餐。
我的Splitted段落的结果应该是
到目前为止,我喜欢这款车,开车很有趣
它作为日常驾驶员非常有效并且有一些好的踢法
虽然距离跑车还很远
我有一个主要问题
沃尔沃在挡风玻璃上建造了加热元件
每隔几毫米的小电线
到了晚上,所有的灯都反射出这些电线并使灯光模糊
这是一个巨大的安全问题,非常烦人
仅由于这个问题
我不确定我是否会将这辆车保留很长时间
虽然加热方向盘很不错
如果你买这辆车,请跳过气候套餐
答案 0 :(得分:2)
您可以尝试这样的事情:
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
答案 1 :(得分:0)
这应该有效:
String reg = "\\ ?[.,()\\[\\]{}]+\\ ?";
String[] res = str.split(reg);
答案 2 :(得分:0)
试试这个正则表达式:
\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