regex problem: convert CSV to comma delimited
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
regex problem: convert CSV to comma delimited
I am reading in a file that is standard CSV, e.g.
option_with_value,option,mm,120,200," one, two, three",174,2,0
I need to convert the commas to tabs, but not of course the commas inside quotation marks. I was trying to write a loop to change any commas inside quotations to, say, ••comma••, then convert other commas to tabs, and convert ••comma•• to a normal comma again.
Here is my replacetext command:
put replacetext (thetext, """(.*),(.*)""", """\1•••comma•••\2""") into thetext
but I get:
compilation error at line 742 (Function: separator is not a ',') near "(.*),(.*)", char 47
Not sure if I am not escaping " correctly, or I can't use "," inside search string. Any help appreciated.
option_with_value,option,mm,120,200," one, two, three",174,2,0
I need to convert the commas to tabs, but not of course the commas inside quotation marks. I was trying to write a loop to change any commas inside quotations to, say, ••comma••, then convert other commas to tabs, and convert ••comma•• to a normal comma again.
Here is my replacetext command:
put replacetext (thetext, """(.*),(.*)""", """\1•••comma•••\2""") into thetext
but I get:
compilation error at line 742 (Function: separator is not a ',') near "(.*),(.*)", char 47
Not sure if I am not escaping " correctly, or I can't use "," inside search string. Any help appreciated.
Kaveh
Re: regex problem: convert CSV to comma delimited
AFAIK,kaveh1000 wrote: put replacetext (thetext, """(.*),(.*)""", """\1•••comma•••\2""") into thetext
you can't use back references in the replace part of replacetext()
But this is not related to the error you mentionned.
Regards,
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: regex problem: convert CSV to comma delimited
Thanks Thierry. By back reference you mean \1, \2?
Kaveh
Re: regex problem: convert CSV to comma delimited
Yep.kaveh1000 wrote:Thanks Thierry. By back reference you mean \1, \2?
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: regex problem: convert CSV to comma delimited
Ok, here is a quick attempt (half tested):kaveh1000 wrote:I am reading in a file that is standard CSV, e.g.
option_with_value,option,mm,120,200," one, two, three",174,2,0
I need to convert the commas to tabs,
but not of course the commas inside quotation marks.
This should work for a one line text entry:
Code: Select all
on mouseUp
put line 1 of field "Ftest" into T
# T: option_with_value,option,mm,120,200," one, two, three",174,2,0
# Regex: \,(?=([^"]*"[^"]*")*[^"]*$)
put replacetext( T, the Regex of me , " - " )
end mouseUp
Or, if you want to process *all* your CVS file:
Code: Select all
on mouseUp
put field "Ftest" into T
# Regex2: (?ms)\,(?=([^"]*"[^"]*")*[^"]*$)
put replacetext( T, the Regex2 of me , " - " )
end mouseUp
Regards,
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: regex problem: convert CSV to comma delimited
I am grateful for this, Thierry...
I did try look-ahead, but my regexp skills were not good enough. This seems to work and I promise to read the code carefully later and fully understand what it is doing.
Thanks again..
I did try look-ahead, but my regexp skills were not good enough. This seems to work and I promise to read the code carefully later and fully understand what it is doing.

Thanks again..
Kaveh
Re: regex problem: convert CSV to comma delimited
You're welcome!kaveh1000 wrote:I am grateful for this, Thierry...
Great!This seems to work and I promise to read the code carefully later and fully understand what it is doing.
Thanks again..
I'll come one of these days in London with a regex quiz..

Regards,
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: regex problem: convert CSV to comma delimited
Just forgot to tell you something..kaveh1000 wrote:... This seems to work...
I've put the regex in a custom property to NOT to fight
with the "devil-escaping-quotes" problem in Livecode which was probably the
reason of the error you did mention in your 1st post.
Regards,
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: regex problem: convert CSV to comma delimited
Yup, I got that, Thierry, and a great trick. I love custom properties of LiveCode. So clever. 

Kaveh
-
- VIP Livecode Opensource Backer
- Posts: 10044
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: regex problem: convert CSV to comma delimited
If you have the option of using any other format you'll be much happier. CSV is notoriously ill-conceived, responsible for the loss of millions of hours every year.
But if you have no choice, Alex Tweedly came up with a good way to handle the insanity that is CSV, which I tweaked for this article on why CSV must die:
http://www.fourthworld.com/embassy/arti ... t-die.html
But if you have no choice, Alex Tweedly came up with a good way to handle the insanity that is CSV, which I tweaked for this article on why CSV must die:
http://www.fourthworld.com/embassy/arti ... t-die.html
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: regex problem: convert CSV to comma delimited
Thanks Richard
Unfortunately no one ever defined CSV properly and I don't like its association with Excel. But it is so simple and human readable. I am looking at alternatives like YAML.
Incidentally there has just been a conference on CSV:
http://csvconf.com/
I will certainly look at your article. I am interested.
(I bought your hypercard book when it first came out!)
Unfortunately no one ever defined CSV properly and I don't like its association with Excel. But it is so simple and human readable. I am looking at alternatives like YAML.
Incidentally there has just been a conference on CSV:
http://csvconf.com/
I will certainly look at your article. I am interested.

(I bought your hypercard book when it first came out!)
Kaveh
Re: regex problem: convert CSV to comma delimited
..........
Last edited by [-hh] on Wed Aug 13, 2014 3:48 pm, edited 1 time in total.
shiftLock happens
Re: regex problem: convert CSV to comma delimited
indeed that is my question and my aim, i.e. regexp is not mandatory. And your solution is a great new approach. I have learned several things already from it. 

Kaveh
-
- VIP Livecode Opensource Backer
- Posts: 10044
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: regex problem: convert CSV to comma delimited
Delimited data can be a great thing, provided the delimiters used aren't as commonly part of the data as are commas.kaveh1000 wrote:Unfortunately no one ever defined CSV properly and I don't like its association with Excel. But it is so simple and human readable.
Tab-delimited is nearly optimal - see the notes near the end of the article for the super-simple format so many millions already use.
Even pipe-delimited is better than CSV.
Heck, tossing all your data out the window and hitting yourself with a hammer is better than CSV.

YAML's great for human-readability, but all formats involve trade-offs and YAML's is with processing time. If human-writability is a key concern it can be worth the time to write a parser, but JSON or even tab-delimited may do quite well depending on your needs.I am looking at alternatives like YAML.
Thanks for the link. I wish they had one in the States. Delimited data is so great for so many things that it makes XML quite obviously bloated and overused for many projects that depend on it. Just any delimiter than a comma, please.Incidentally there has just been a conference on CSV:
http://csvconf.com/

I wish I could say "Thanks", but I'm afraid you must be thinking of one of the more accomplished contributors to these forums. My only printed XTalk work was the SuperTalk Language Guide, and I've not written a HyperCard book.(I bought your hypercard book when it first came out!)
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: regex problem: convert CSV to comma delimited
Hi Richard
I thought I must be missing something but it is now so obvious that comma is the worst possible delimiter, and quotes can get you in a mess too. I actually had a tab delimited "CSV" file but I wanted it to more "standard". Should have just kept it the way it was! Might go back.
And sorry for mixing you up. My memory is associating you with HyperCard for ever...
I thought I must be missing something but it is now so obvious that comma is the worst possible delimiter, and quotes can get you in a mess too. I actually had a tab delimited "CSV" file but I wanted it to more "standard". Should have just kept it the way it was! Might go back.
And sorry for mixing you up. My memory is associating you with HyperCard for ever...

Kaveh