Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 27th, 2012, 01:20 PM
mikeysweet mikeysweet is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 3 mikeysweet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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:

Code:
(?<=[\d\$]),


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.

Reply With Quote
  #2  
Old June 27th, 2012, 02:02 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,690 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 3 h 43 m 47 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
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)?

Reply With Quote
  #3  
Old June 27th, 2012, 02:17 PM
mikeysweet mikeysweet is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 3 mikeysweet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old June 28th, 2012, 06:51 AM
abareplace abareplace is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 29 abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 8 h 25 m 9 sec
Reputation Power: 0
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.

Reply With Quote
  #5  
Old June 28th, 2012, 09:54 AM
mikeysweet mikeysweet is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 3 mikeysweet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Code:
,(?=[^{}]*\})

Reply With Quote
  #6  
Old June 28th, 2012, 12:33 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,690 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 3 h 43 m 47 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
I was also thinking it might be easier to replace the {}s with quotes.

Reply With Quote
  #7  
Old June 28th, 2012, 03:02 PM
spacebar208's Avatar
spacebar208 spacebar208 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: spaceBAR Central
Posts: 188 spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 9 h 10 m 2 sec
Reputation Power: 41
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Help to find/replace all commas within curly brackets

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap