Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegExp to find/replace newlines within double quotes, not affecting newlines outside double-quotes

Tags:

regex

php

csv

I've got a CSV file which gives problems importing because of the fields containing new line characters. As CSV importers treat every newline as a new row, the newlines in the fields mess things up.

So I want to replace every newline within double quotes with <br> leaving the 'real' newlines outside double quotes untouched.

First step would be to be able to create a regexp to get the newlines within the text file.

(\n|\r)

But after this I get lost, because I can't find the xxx within this expression:

(")(xxx)(\n|\r)(xxx)(") 

And a solution to let the 'real' newlines untouched.

Concluding: How can I replace every newline within a field, enclosed by double quotes with a <br>

I'm now using sublime text to test the regex but later it will be used in a php application.

like image 880
stUrb Avatar asked Feb 12 '23 19:02

stUrb


1 Answers

Search pattern:

("[^"\n]*)\r?\n(?!(([^"]*"){2})*[^"]*$)

Replacement Pattern:

$1<br>

RegEx Demo

like image 142
anubhava Avatar answered Feb 14 '23 09:02

anubhava