The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
Help to find/replace all commas within curly brackets
Discuss Help to find/replace all commas within curly brackets in the Regex Programming forum on Dev Shed. Help to find/replace all commas within curly brackets Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 27th, 2012, 01:20 PM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 3
Time spent in forums: 28 m 7 sec
Reputation Power: 0
|
|
|
Help to find/replace all commas within curly brackets
I'm bagging my head on my keyboard trying to see if that would help me, but it's not.
Issue: I have a CSV file that includes a couple of fields that begin and end with curly brackets. Within those brackets, there are commas. Below is a line from this CSV file:
Code:
"A",3,"Bqt- Open Music","Open","Music","Bqt- Open Music",{1,$0.00,2,$20.00,3,$30.00},92,8,3,0,0,0,0,{},,$0.00,0,1,1,1,1,0,0,15,"Open Music",1,0,{},{17,1,18,0,19,0,26,0,27,0,28,0,29,0,30,0,33,0,34,0,35,0,36,0,37,0,38,0,39,0,40,0},0,0
I need to find the commas (,) ONLY within the curly brackets and turn them into pipes (|)
So far, all I can figure out is how to find commas that have digits or dollar sign ($) around them:
I would like the end results to look like this:
Code:
"A",3,"Bqt- Open Music","Open","Music","Bqt- Open Music",{1|$0.00|2|$20.00|3|$30.00},92,8,3,0,0,0,0,{},,$0.00,0,1,1,1,1,0,0,15,"Open Music",1,0,{},{17|1|18|0|19|0|26|0|27|0|28|0|29|0|30|0|33|0|34|0|35|0|36|0|37|0|38|0|39|0|40|0},0,0
Thanks for all your help with this.
|

June 27th, 2012, 02:02 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
Are you converting them away from commas so that your CSV parsing thing won't misinterpret them as field delimiters? If so, what parser (ie, what's reading the file)?
|

June 27th, 2012, 02:17 PM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 3
Time spent in forums: 28 m 7 sec
Reputation Power: 0
|
|
|
Try not to laugh too much, but I need to open this up in Excel. I have a client who will need to edit the items and it's easier for him to just edit while in Excel.
BUT, your question has peaked my interest. Is there an application that would be better suited to open this in? I'm perfectly happy to open using a different application, replace the troublesome commas with a pipe, re-save as a CSV, and then open it in Excel. Unfortunately, I don't know of a robust application that could distinguish commas within brackets and field-delimiting commas.
|

June 28th, 2012, 06:51 AM
|
|
|
You need to search for a comma that is preceded by opening brace. Additionally, there is no closing brace between the opening brace and the comma.
Code:
Search for: (?<={[^}]+),
Replace to: |
This solution uses variable-length lookbehind, which is supported by .NET and several other tools, including my program. Unfortunately, it would not work in PHP or Python.
|

June 28th, 2012, 09:54 AM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 3
Time spent in forums: 28 m 7 sec
Reputation Power: 0
|
|
This morning, a coworker who was also thinking about this drafted the following regex and it works perfectly. Thanks for your help!
|

June 28th, 2012, 12:33 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
I was also thinking it might be easier to replace the {}s with quotes.
|

June 28th, 2012, 03:02 PM
|
 |
Contributing User
|
|
Join Date: Apr 2012
Location: spaceBAR Central
|
|
The left/right curly braces could be deleted and the commas changed to a TAB and then they should be able to open the file with excel and it will place each value into a column:
Code:
sed 's/[{|}]//g;s/,/\t/g' test.txt > test.xls
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|