How to split a line based on punctuation and spaces?

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

+7
source share
3 answers

Code with some oddity handling: (Note that it skips empty tokens in the output loop. It's quick and dirty.) You can add all the characters that you need to split and delete into the regular expression pattern. (tchrist is right. This thing is filled with bitterness and works only in 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 
+14
source

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 */ 
0
source
 str.split(" ,.!?;") 

will be a good start for English. You need to improve it based on what you see in your data and what language you use.

-3
source

All Articles