-2

I need to split sentence to words and punctuation marks, and place em into list, saving their sequence.

For example: "Some text here!". And result should be: List(Some, ,text, , here,!)

I'm using String.split("regex"); With "split" I can split text only by word or only by punctuation.

So what should I use, to split text by words and punctuation at same time? Thank you in advance.

erhun
  • 3,549
  • 2
  • 35
  • 44
Mista Jam
  • 3
  • 2
  • what if there are some digits? .. `some text2 here! 4 you`? – TheLostMind Mar 25 '15 at 12:37
  • digits belong to words. It's like to split words from non word characters. – Mista Jam Mar 25 '15 at 12:40
  • Now I only can split string to words and add them to list. Like String[] words = sentenseToParse.split(regex); But I also need to get non word characters like !@#$ from same string (including spaces). And place em to String[] sentenseParts. It's should be like {word, space, another word, dot} – Mista Jam Mar 25 '15 at 12:44

1 Answers1

0

Based on

And result should be: List(Some, ,text, , here,!)

it looks like you want to split on word boundaries split("\\b").

String data = "Some text here!";
for (String s : data.split("\\b")){
    System.out.println("'"+s+"'");
}

Output:

'Some'
' '
'text'
' '
'here'
'!'
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • yes, that's what I need. Ill try it out now. Thank you! And sorry for my stupid question) – Mista Jam Mar 25 '15 at 12:47
  • That kind of complicates things too much to be answered in this question. Consider [posting another question](http://stackoverflow.com/questions/ask) in which you will describe precisely what you want to achieve. – Pshemo Mar 25 '15 at 13:02
  • Also try to avoid [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Pshemo Mar 25 '15 at 13:05
  • Thank u. I will. I realise that I ducked up with question. – Mista Jam Mar 25 '15 at 13:15