我试图将一个小段落分割成基于句号和副词的句子,比如() {}和[]。如何使用做这样的事情?
例如,如果我有一个段落,如
到目前为止,我喜欢这辆车,开车很有趣。虽然离跑车还有很远的距离,但作为一名日常车手,它工作得很好,而且踢得很好。我有一个主要的问题,这个car.Volvo已经内置加热元件到挡风玻璃(每几毫米小电线)。晚上,所有的灯都从这些电线反射出来,使灯光模糊不清。这是一个巨大的安全问题,是非常恼人的。由于这个问题,我不确定我是否会保留这辆车很长时间。虽然有一个加热的方向盘是很好的,如果你买这辆车,跳过气候包。
我分裂段落的结果应该是
到目前为止,我喜欢这辆车,开车很有趣。 作为一名每日车手,它工作得很好,而且踢得很好。 虽然离跑车还有很远的距离 我对此有一个大问题 沃尔沃在挡风玻璃上安装了加热元件。 每隔几毫米就有一条小电线 到了晚上,所有的灯都从这些电线反射出来,使灯光模糊不清。 这是一个巨大的安全问题,而且非常烦人。 仅仅因为这个问题 我不确定我是否会把这辆车保留很久。 虽然有一个加热的方向盘很好 如果你买了这辆车,就跳过气候包
发布于 2015-02-23 07:47:39
你可以试试这样的方法:
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发布于 2015-02-23 07:48:28
这应该是可行的:
String reg = "\\ ?[.,()\\[\\]{}]+\\ ?";
String[] res = str.split(reg);发布于 2015-02-23 07:55:20
试试这个正则表达式:
\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 carhttps://stackoverflow.com/questions/28668920
复制相似问题