Regex Wrapping Quotes

Dan W Source

I am trying to wrap quotes around certain section of content in a CSV file, the current layout is something like this:

 ###element1,element2,element3,element4,element5,element6,element7,element8, "element9,
element9,""element9"",element9,
element9,element9,""element9",element10,
###

the ### symbols depict a new line and each new line should have one, the problem is I need to get to all of element 9 in to one set of double quotes, however there are multiple instances of doublequotes within that area which break up the element in to new fields making my table expand beyond the fields I initially set. So I believe I need to remove all the " marks between the start and end of element9 and then reintroduce one set to highlight the whole section.

I approached this firstly by trying to select the 8th Comma from the start and the 2 comma from the end:

 ^((?:[^,]+,){8})(.+)((?:,[^,]*){2})$

and replacing with

$1"$2"$3

I tried to target the starting ### and ending ### to select those two elements but with no success.

any suggestions on how I can do this

UPDATE

    ###BLAHBLAH,BLAHBLAH,BLAHBLAH,BLAHBLAH,BLAHBLAH,BLAHBLAH,BLAHBLAH,BLAHBLAH,BLAHBLAH,
BLAHBLAH,
BLAHBLAH,
BLAHBLAH, BLAHBLAH,
BLAHBLAH, BLAHBLAH,
BLAHBLAH,
"BLAHBLAH""",E,
###

The last field always seem to contain a capital letter, the fields before vary in quotation placement so to really target that whole section I need to work out how many commas along and how many back I need to go, remove the quotes and then reinstate them in the correct positions.

regexcsvnotepad++

Answers

answered 3 years ago Jeff Y #1

/^(?:[^,]*,){8}([^#]*),[^,]*,[^,]*$/s

https://regex101.com/r/hU8yO6/1

I think the regexp you had is about right, except for needing the /s modifier.

For notepad++, get the s modifier by ticking ". matches newline":

^(?:[^,]*,){8}([^#]*),[^,]*,[^,]*$

This looks like a good reference: http://docs.notepad-plus-plus.org/index.php/Regular_Expressions

You'll probably want to add parens appropriately to make capture groups also.

answered 3 years ago vks #2

###(?:[^,]*,){8}\K([\s\S]*?)(?=,[^,]*,[^,]*?###)

Try this.Replace by "\1" or "$1".See demo.

https://regex101.com/r/tD0dU9/13

answered 3 years ago aravind #3

^#+[^"]+"([^#]+),[^,]+,[^,]+###\s*$

comments powered by Disqus