0

I'm looking for regex to do the following in Java:

String originalString = "";
String splitString[] = originalString.spilt(regex);

Some test cases:

Original1: foo bar "simple"
Spilt1: { "foo", "bar", "\"simple\"" }

Original2: foo bar "harder \"case"
Spilt2: { "foo", "bar", "\"harder \"case\"" }

Original3: foo bar "harder case\\"
Spilt3: { "foo", "bar", "\"harder case\\"" }

Some snippets I have come across:

# Does not react to escaped quotes
 (?=([^\"]*\"[^\"]*\")*[^\"]*$)
# Finds relevant quotes that surround args
(?<!\\)(?:\\{2})*\"

Thanks!

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
deadfire19
  • 329
  • 2
  • 16
  • 1
    Is there a question in there I'm not seeing? – Andreas May 06 '16 at 23:23
  • @Andreas I'm looking for regex that will find the whitespaces that the strings need to be split on. – deadfire19 May 06 '16 at 23:27
  • 1
    I voted to close your question as duplicate, but since pointed solution is not using `split` method but `Matcher#find` feel free to let me know if you didn't want to use it and want to have your question reopened for purely `split(regex)` solution. – Pshemo May 07 '16 at 00:07
  • 1
    @Pshemo The solution you linked works really well. Thanks a lot! – deadfire19 May 07 '16 at 00:17

2 Answers2

1

Regex like this will work for simple cases:

("(.+?)(?<![^\\]\\)")|\S+

But I would not suggest to use RegEx for this task, but take a look at CSV parsers instead.

Kiryl
  • 111
  • 4
0
String stringToSplit = This is the string to split;

String[] split = stringToSplit.split("character to split at");

In this case, split[0] would result as ' This ', split[1] would be ' is ', split[2] would be ' the ', split[3] ' string ' split[4] ' to ' split[5] ' split '.

At this point you can do

var0 = split[0];
var1 = split[1];
var2 = split[2];

Where var0 would equal "This" And so on...

Hope this helps.

Ethan Moore
  • 383
  • 1
  • 5
  • 18