开发者

Notepad++ regex replace - replace all commas with \, within quotations

开发者 https://www.devze.com 2023-03-14 01:08 出处:网络
I am trying to import a csv file into mysql, and I need to convert it into a proper format before importing.

I am trying to import a csv file into mysql, and I need to convert it into a proper format before importing.

If there's a comma in a column, the csv encloses it within double quotations, here's an example of a row without a comma, and a row with a comma:

1,Superman
2,"Batman,Flash"

What I need to do is to convert all columns which have commas to escape the comma and remove the quotations... such as "Batman,Flash" to Batman\,Flash

Here's what I have so far

Find: "(.*),(.*)"
Replace: \1\\,\2

However, there are two cases in which this does not work:

  • It will only replace one comma if there's more than one comma withing a quoted column. So something like "Batman,Flash,Robin" will be converted to Batman,Flash\,Robin
  • This doesn't work if the first column has a comma as well. For example, on a row such as "1,2,3","Batman,Robin"

How开发者_如何学编程 can I change the regexes to accommodate the two cases that don't yet work?


I'm sorry, but regex is not the tool for this. You must parse it.

Why?

Do you want to convert this?

"test\, w00t!"

Or what about this?

"test\\\\\, w00t!"

Heck, even this?

"tes\\","\"ing\,\\,"
0

精彩评论

暂无评论...
验证码 换一张
取 消