javaregexparagraph

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


I am trying to split a small paragraph in to sentences based on full stop commas and parantheses like () {} and []. How to do such thing using Java Regex

For example if I have a Paragraph like

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.

My result of Splitted paragraph should be

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


Solution

  • You could try something like this:

    String str = "...";
    str = str.replaceAll(" ?[,.()]+ ?", System.getProperty("line.separator"));
    

    If you want an array, use this:

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

    Yields:

    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