I write some java code to split string into array of string. First, I split that string using regex pattern \"\\\\,\\\\,|\\\\,\"
and then I split using pattern
Seeing the two results, it seems that the split method try to find the first expression at first ("," for regex2, ",," for regex1) and split the string, and then the second one, but after the first pass with regex2 there isn't a single "," left in the strings. That's why there is an empty string detected when ",," is read with regex2.
So for your regex to be useful, you need to write the more complex expression first.
How regex works: The state machine always reads from left to right. ,|,,
== ,
, as it always will only be matched to the first alternation:
(source: gyazo.com)
,,|,
== ,,?
:
(source: gyazo.com)
However, you should use ,,?
instead so there's no backtracking:
(source: gyazo.com)
It will be evaluated from left to right. In regex1
, \\,\\,
is tried first, otherwise \\,
is tried. That's why 12th String is not empty, because \\,\\,
is matched in that case. For regex2
, everything is matched using \\,
, hence the empty String.
Case 1: Split by ,,
else ,
This gets only first case, the rest split by ,
.
Case 2: Split by ,
else ,,
gets all cases. So ,,
gets split into word
and ,word
.
Then ,word
gets split into " " and word
.