-1

Is it possible to use regex to split by words instead of symbol/special characters?

For instance, It is fun, content=[I love java, content=[Java eclipse then split by content=[

So the results will become like

It is fun

I love java

Java eclipse

I tried something like this but it have error that say HTTP Status 500 - illegal repetition near index

String [] strArray = testcontent.split("content=[");
                    for (String str :strArray) {
                    System.out.println("TestSplit" + str);

1 Answers1

2

Split accepts a regexp so you can use whatever expression you want, just be careful to escape special characters.

split("content\\=\\[")  

or

 split(Pattern.quote("content=["))
Carlos Verdes
  • 3,037
  • 22
  • 20
  • 2
    Or if you don't want to play around with backslashes, `split(Pattern.quote("content=["))`, [according to this answer](http://stackoverflow.com/a/6065932/1270789). – Ken Y-N Oct 25 '16 at 05:46
  • Thanks @KenY-N I didn't know that!! – Carlos Verdes Oct 25 '16 at 05:47
  • Thank you, it worked. Sorry so how do I split SolrDocument{content=[? Is it something like this? split(" SolrDocument\ \{\ \content\ \=\ \[ ") @Carlos – Have a lot of question Oct 25 '16 at 05:49
  • Thank you so much too, just one more question. Is it possible to forward the String to JSP using getAttribute and printed out in JSP? @KenY-N – Have a lot of question Oct 25 '16 at 05:52
  • @Havealotofquestion I think you should read about regexp, when you want to check different patterns you can use |, for example split("content|SolrDocument"). – Carlos Verdes Oct 25 '16 at 05:59
  • Thank you will read more as I am new to this and java too. Hmm, one more question when I printed on the console it did show the split but why it only display one results when I forward the String to JSP using getAttribute method @CarlosVerdes – Have a lot of question Oct 25 '16 at 06:08
  • I think you should open another question for this ,) If you like my answer please mark as resolver :) – Carlos Verdes Oct 25 '16 at 08:35