cl-r
cl-r

Reputation: 1264

Using regex to remove quote

I saw a good sample, but I cannot adapt it for my problem.

I would like to remove only enclosing field " from a CSV line like :

" kkl ";"aa bb D";;12 "AA";;"SSS"-;" gg 12";" vv";"sdqs ";

expected result :

 kkl ;aa bb D;;12 "AA";;"SSS"-; gg 12; vv;sdqs ;

I use Pattern and Matcher tools

Upvotes: 0

Views: 303

Answers (2)

nhahtdh
nhahtdh

Reputation: 56829

This solution assumes that there is no escaped quote \" in the quoted string

.replaceAll("(?<=^|;)\"([^\"]*?)\"(?=;|$)", "$1")

I assume that you also want to strip off the " in these case: "sdfkjhksdf", ;;;"dffff"

Another solution uses possessive quantifier, whose effect relies on the assumption that " doesn't appear inside the quoted portion.

.replaceAll("(?<=^|;)(?:\"(.*?)\"){1}+(?=;|$)", "$1")

Upvotes: 5

Jon Lin
Jon Lin

Reputation: 143966

Small modification to @nhahtdh's regex in order to keep it from greedily matching outside of a CSV boundary:

.replaceAll("(?<=^|;)\"([^;]*)\"(?=;|$)", "$1");

Upvotes: 2

Related Questions