Years ago I saw this problem asked of Jeffrey Friedl (author of
Mastering Regular Expressions) in an OSCON session where he invited
folks to provide problems to solve. I didn't write down the final
result, but it was a multi-line expression that took him (and members of
the group) most of the hour to get to.
I think an "elegant" regex might be out of the question for this.
Simon R wrote:
> OK... I've been toying with this for a day now and it's time to set the
> experts loose. I need to be able to split a string of the form
>
> foo,bar',name=simon,age=99
>
> into its constituent parts (comma is the delimeter). So far so good and
> very simple using split(). However, I also need to be able to handle the
> case where there may be commas in a phrase enclosed by quote marks, in which
> case we should NOT split on those commas;
>
> e.g.
>
> foo,bar,names="charlie,john,ann",ages='20,30,40' must be split to
>
> foo
> bar
> names="charlie,john,ann"
> ages='20,30,40'
>
> Can anyone come up with a single regexp that I can use in a call to split()
> that will handle this case, or another equally elegant solution ?
>
> TIA
>
> - Simon Rosenthal
> Northern Light Technology LLC
>
> _______________________________________________
> Boston-pm mailing list
> Boston-pm-PqP1ghmmPMdAfugRpC6u6w@xxxxxxxxxxxxxxxx
> http://mail.pm.org/mailman/listinfo/boston-pm
>
|