andandandand
andandandand

Reputation: 22270

How to split a string based on punctuation marks and whitespace?

I have a String that I want to split based on punctuation marks and whitespace. What should be the regex argument to the split() method?

Upvotes: 7

Views: 9878

Answers (3)

Paul Sasik
Paul Sasik

Reputation: 81509

Code with some weirdness-handling thrown in: (Notice that it skips empty tokens in the output loop. That's quick and dirty.) You can add whatever characters you need split and removed to the regex pattern. (tchrist is right. The \s thing is woefully implemented and only works in some very simple cases.)

public class SomeClass {
    public static void main(String args[]) {
        String input = "The\rquick!brown  - fox\t\tjumped?over;the,lazy\n,,..  \nsleeping___dog.";

        for (String s: input.split("[\\p{P} \\t\\n\\r]")){
            if (s.equals("")) continue;
            System.out.println(s);
        }
    }
}


INPUT:

The
quick!brown  - fox      jumped?over;the,lazy
,,..  
sleeping___dog.

OUTPUT:

The
quick
brown
fox
jumped
over
the
lazy
sleeping
dog

Upvotes: 16

Fseee
Fseee

Reputation: 2627

try something like this:

String myString = "item1, item2, item3";
String[] tokens = myString.split(", ");
for (String t : tokens){
            System.out.println(t);
        }

/*output
item1
item2
item3
*/

Upvotes: 0

rahul
rahul

Reputation: 1290

str.split(" ,.!?;") 

would be a good start for english. You need to improve it based on what you see in your data, and what language you're using.

Upvotes: -3

Related Questions