In Java what should be the regex, if i need the text withing the quotes as one element? -
my input string is:
apple, orange, "banana,cherry", peach
i need output as:
apple orange banana,cherry peach
i have tried using regex as:
",(?=([^\"]*\"[^\"]*\")*[^\"]*$)", -1
...in split()
method of java, output as:
apple, orange, "banana, cherry" peach
this 1 should work, though regex should figure out yourself. gods if ever want change in it; since didn't design monster you'll have no clue how works.
and monster is, believe me.
\s*("?)\s*([^",]+?(\s*,\s*[^",]+?)*?)?\s*\1\s*(,|$)
basically, puts in optional quote @ start , makes capture group out of it, ("?)
, , uses backreference \1
later in regex make sure matched content should either surrounded quotes, or not have them @ all.
the rest whitespace , comma-separated content of format "optional stuff" + "repeating optional stuff starting comma", using "not quote or comma" group [^",]
indicate actual text content match.
capture group #2 contain data between commas need, trimmed @ both sides, , taking stuff between quotes 1 match.
note out of necessity capture text behind final comma, forced make matched content end on "comma or end of line", means multi-line content between quotes (which exists in excel csv output) not matched correctly.
Comments
Post a Comment