For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2004 > Escaping









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Escaping
Malloc7c3@aol.com

2004-04-08, 6:31 pm

Is there any quick way of parsing a string like:
This,Is,A,String,\,,With,A,Comma

Into a list
("This", "Is", "A", "String", ",", "With, "A", "Comma")

Basically, how can I split it by commas, except when it is escaped.

Dan

James Edward Gray II

2004-04-08, 6:31 pm

On Apr 8, 2004, at 4:30 PM, Malloc7c3@aol.com wrote:

> Is there any quick way of parsing a string like:
> This,Is,A,String,\,,With,A,Comma
>
> Into a list
> ("This", "Is", "A", "String", ",", "With, "A", "Comma")
>
> Basically, how can I split it by commas, except when it is escaped.


How about :

my @fields = split m/(?<!\\),/, 'This,Is,A,String,\\,,With,A,Comma';
print join('~', @fields), "\n";

That's a negative lookbehind assertion in the regex I fed split().

If you made the original string format, you might consider using the
CSV file format instead. Then you can even use modules to do all the
tricky stuff.

James

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com