Since this may require recursion (as you say the values may be key/value
pairs themselves), you'll need something a bit more powerful than a
regex. I suggest looking into Text::Balanced, which can do what you
want.

Here's sample code to get you started. It does the first step of
extracting the outermost pairs.

#-------
use Text::Balanced qw(extract_multiple extract_bracketed);

my $str = '{key1,val1},{key2,val2},{key3,{key3.1,val3.1}},{key4,val4}';

my @pairs = extract_multiple (
                              $str,
                              [
                               sub { extract_bracketed($_[0],'{}') },
                              ],
                              undef, # no maximum
                              1,     # skip separator commas
                             );

print join "\n", @pairs;
#-------

- Mark. 

> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On 
> Behalf Of Darryl Ross
> Sent: Wednesday, November 16, 2005 11:10 PM
> To: perl-unix-users@listserv.ActiveState.com
> Subject: [Perl-unix-users] Help Needed With a Regex
> 
> Hey All,
> 
> I have a string that looks like this:
> 
> {key1,val1},{key2,val2},{key3,val3},{key4,val4},{key5,val5},...
> 
> I'm trying to write a regular expression which can split up 
> that string
> and give me a hash back. There are a few gotchas though:
> 
>     - The key and/or the value may or may not be inside quotes,
> 
>     - The key and/or the value may contain a comma (it will 
> be quoted if
>       it does),
> 
>     - There can be a varying number of key/value pairs, including only
>       one pair,
> 
>     - The values may be key/value pairs themselves.
> 
> My first attempt was something like: /({[^}]+,[^}]+})+/
> 
> But that completely fails at least the last point, if not the others.
> 
> Does anyone have any ideas (or pointers to good documentation)?
> 
> TIA,
> Darryl
> 
> 


_______________________________________________
Perl-Unix-Users mailing list
Perl-Unix-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to