Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting around an outer character in Perl. Minimal assumptions within group

Tags:

regex

perl

I am having a hard time adapting the answer in this thread to the following problem:

I would like to split the following string:

my $string = "foo{age}, bar{height}. something_else. baz{weight,so='yes',brothers=john.smith}.test{some}"

around the outer dots. The result should be an array holding

("foo{age}, bar{height}", 
 "foo{weight,parents='yes',brothers=john.smith}", 
 "test{some}")

I would like to avoid making assumptions about what's inside the groups inside {}.

How can I do this in Perl?

I tried adapting the following:

print join(",",split(/,\s*(?=\w+{[a-z,]+})/g, $string));

by replacing what's inside the character class [] without success.

Update:

The only characters not allowed within a {} group are { or }

like image 590
Amelio Vazquez-Reina Avatar asked Dec 10 '25 10:12

Amelio Vazquez-Reina


1 Answers

Since you are not dealing with nested braces, the periods you want are those which are not "immediately" followed by a closing }. Where "immediately" means, without an opening { in between:

split(/[.]\s*(?![^{]*[}])/g, $string)

Alternatively, to match the parts you're interested in:

(?:[^.{}]|[{][^{}]*[}])+

Which can be "unrolled" to:

[^.{}]*(?:[{][^{}]*[}][^.{}]*)*
like image 140
Martin Ender Avatar answered Dec 12 '25 00:12

Martin Ender