java - java中根据特殊字符和括号分割段落

标签 java regex pattern-matching paragraph

我试图根据句号逗号和括号(如 () {} 和 [])将一小段分成句子。如何使用 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

关于java - java中根据特殊字符和括号分割段落,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28668920/

相关文章:

java正则表达式,读取文件夹

javascript - 正则表达式: Help Matching a number less than 24

hashmap - 模式匹配选项时,引用类型不兼容的匹配臂引发错误

java - 将句子中的单个单词大写

java - NoClassDefFoundError : Tomcat 7 unable to load classes from jar

windows - 为什么 findstr 不能正确处理大小写(在某些情况下)?

javascript - 创建一个 Javascript RegExp 以在 HTML/php 模板中查找开始标签

java - 为微调器中的字符串数组项分配值

java - 以编程方式触发完整的堆栈转储?

r - R中的剪切函数替代