Matthew
Matthew

Reputation: 817

Escape all commas in line except first and last

I have a CSV file which I'm trying to import to a SQL Server table. The file contains lines of 3 columns each, separated by a comma. The only problem is that some of the data in the second column contains an arbitrary number of commas. For example:

1281,I enjoy hunting, fishing, and boating,smith317

I would like to escape all occurrences of commas in each line except the first and the last, such that the result of this line would be:

1281,I enjoy hunting\, fishing\, and boating,smith317

I know I will need some type of regular expression to accomplish this task, but my knowledge of regular expressions is very limited. Currently, I'm trying to use Notepad++ find/replace with regex, but I am open to other ideas.

Any help would be greatly appreciated :-)

Upvotes: 0

Views: 275

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167212

Okay, could be a manual stuff. Do this:

  1. Normal find all the , and replace it with \,. Escape everything.
  2. Regex find ^(.*)(\\,) and replace it with $1,.
  3. Regex find (\\,)(.*)$ and replace it with ,$2.

Worked for me in Sublime Text 2.

Upvotes: 2

Related Questions