Escaping a comma-delimited line.

View: New views
4 Messages — Rating Filter:   Alert me  

Escaping a comma-delimited line.

by Peter Boughton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have some data resembling the following:
Mr,Bob,Smith,Bobs House,Bobs Road,,,Bobs City,,Bobs Postcode,"China,
People's Republic of",bob@...,,

And I would like to convert it to this:
"Mr","Bob","Smith","Bobs House","Bobs Road","~BLANK~","~BLANK~","Bobs
City","~BLANK~","Bobs Postcode","China~COMMA~ People's Republic
of","bob@...","~BLANK~","~BLANK~"

Can anyone provide a/some tidy regex statement(s) that will do this for me.

Bonus points for being able to cater for things such as this:
Mr,Bob,Smith,Bobs "Fun" House,Bobs Road...
to
"Mr","Bob","Smith","Bobs ~QUOTE~Fun~QUOTE~ House","Bobs Road"...
but I don't think that's needed for my current set of data.

A thousand thankyou's to anyone able to help.

Peter

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Discover CFTicket - The leading ColdFusion Help Desk and Trouble
Ticket application

http://www.houseoffusion.com/banners/view.cfm?bannerid=48

Message: http://www.houseoffusion.com/lists.cfm/link=i:21:919
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/21
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:21
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=17837.14401.21
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54


Re: Escaping a comma-delimited line.

by Ben Doom :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Well, this can be done with regex, but I would probably skip it.  Check
first and last chars, and prepend/append ~BLANK~ as necessary, replace()
quotes with ~QUOTE~, replace commas with "," and then prepend/append
with quotes.  Something like

The code might not be as pretty, but it'll be easier to debug.

--Ben

Peter Boughton wrote:

> I have some data resembling the following:
> Mr,Bob,Smith,Bobs House,Bobs Road,,,Bobs City,,Bobs Postcode,"China,
> People's Republic of",bob@...,,
>
> And I would like to convert it to this:
> "Mr","Bob","Smith","Bobs House","Bobs Road","~BLANK~","~BLANK~","Bobs
> City","~BLANK~","Bobs Postcode","China~COMMA~ People's Republic
> of","bob@...","~BLANK~","~BLANK~"
>
> Can anyone provide a/some tidy regex statement(s) that will do this for me.
>
> Bonus points for being able to cater for things such as this:
> Mr,Bob,Smith,Bobs "Fun" House,Bobs Road...
> to
> "Mr","Bob","Smith","Bobs ~QUOTE~Fun~QUOTE~ House","Bobs Road"...
> but I don't think that's needed for my current set of data.
>
> A thousand thankyou's to anyone able to help.
>
> Peter
>
>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Find out how CFTicket can increase your company's customer support
efficiency by 100%
http://www.houseoffusion.com/banners/view.cfm?bannerid=49

Message: http://www.houseoffusion.com/lists.cfm/link=i:21:920
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/21
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:21
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=17837.14401.21
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54


Re: Escaping a comma-delimited line.

by Peter Boughton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Was there an example there? "Something like..."?

I've currently got something which works similarly to how you
described, but the bit which is causing problems is when there is a
comma inside an already quoted field - in my example, the "China,
people's republic of" bit.
It seems like a simple ,"[^"]*,[^"]*", should work, ... and it does!
:S Gah, it didn't work earlier. :@

On 12/08/05, Ben Doom <bdoom-hof@...> wrote:

> Well, this can be done with regex, but I would probably skip it.  Check
> first and last chars, and prepend/append ~BLANK~ as necessary, replace()
> quotes with ~QUOTE~, replace commas with "," and then prepend/append
> with quotes.  Something like
>
> The code might not be as pretty, but it'll be easier to debug.
>
> --Ben
>
> Peter Boughton wrote:
> > I have some data resembling the following:
> > Mr,Bob,Smith,Bobs House,Bobs Road,,,Bobs City,,Bobs Postcode,"China,
> > People's Republic of",bob@...,,
> >
> > And I would like to convert it to this:
> > "Mr","Bob","Smith","Bobs House","Bobs Road","~BLANK~","~BLANK~","Bobs
> > City","~BLANK~","Bobs Postcode","China~COMMA~ People's Republic
> > of","bob@...","~BLANK~","~BLANK~"
> >
> > Can anyone provide a/some tidy regex statement(s) that will do this for me.
> >
> > Bonus points for being able to cater for things such as this:
> > Mr,Bob,Smith,Bobs "Fun" House,Bobs Road...
> > to
> > "Mr","Bob","Smith","Bobs ~QUOTE~Fun~QUOTE~ House","Bobs Road"...
> > but I don't think that's needed for my current set of data.
> >
> > A thousand thankyou's to anyone able to help.
> >
> > Peter
> >
> >
>
>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Find out how CFTicket can increase your company's customer support
efficiency by 100%
http://www.houseoffusion.com/banners/view.cfm?bannerid=49

Message: http://www.houseoffusion.com/lists.cfm/link=i:21:921
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/21
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:21
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=17837.14401.21
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54


Re: Escaping a comma-delimited line.

by Ben Doom :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Peter Boughton wrote:
> Was there an example there? "Something like..."?

"that" was supposed to follow.

> I've currently got something which works similarly to how you
> described, but the bit which is causing problems is when there is a
> comma inside an already quoted field - in my example, the "China,
> people's republic of" bit.
> It seems like a simple ,"[^"]*,[^"]*", should work, ... and it does!
> :S Gah, it didn't work earlier. :@

Hah!  I think we've all had that happen.  :-)

--Ben

> On 12/08/05, Ben Doom <bdoom-hof@...> wrote:
>
>>Well, this can be done with regex, but I would probably skip it.  Check
>>first and last chars, and prepend/append ~BLANK~ as necessary, replace()
>>quotes with ~QUOTE~, replace commas with "," and then prepend/append
>>with quotes.  Something like
>>
>>The code might not be as pretty, but it'll be easier to debug.
>>
>>--Ben
>>
>>Peter Boughton wrote:
>>
>>>I have some data resembling the following:
>>>Mr,Bob,Smith,Bobs House,Bobs Road,,,Bobs City,,Bobs Postcode,"China,
>>>People's Republic of",bob@...,,
>>>
>>>And I would like to convert it to this:
>>>"Mr","Bob","Smith","Bobs House","Bobs Road","~BLANK~","~BLANK~","Bobs
>>>City","~BLANK~","Bobs Postcode","China~COMMA~ People's Republic
>>>of","bob@...","~BLANK~","~BLANK~"
>>>
>>>Can anyone provide a/some tidy regex statement(s) that will do this for me.
>>>
>>>Bonus points for being able to cater for things such as this:
>>>Mr,Bob,Smith,Bobs "Fun" House,Bobs Road...
>>>to
>>>"Mr","Bob","Smith","Bobs ~QUOTE~Fun~QUOTE~ House","Bobs Road"...
>>>but I don't think that's needed for my current set of data.
>>>
>>>A thousand thankyou's to anyone able to help.
>>>
>>>Peter
>>>
>>>
>>
>>
>
>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Discover CFTicket - The leading ColdFusion Help Desk and Trouble
Ticket application

http://www.houseoffusion.com/banners/view.cfm?bannerid=48

Message: http://www.houseoffusion.com/lists.cfm/link=i:21:922
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/21
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:21
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=17837.14401.21
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54