This question already has an answer here:
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!
Regex like this will work for simple cases:
But I would not suggest to use RegEx for this task, but take a look at CSV parsers instead.
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
Where
var0
would equal "This" And so on...Hope this helps.