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.
The only characters not allowed within a {} group are { or }
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:
[^.{}]*(?:[{][^{}]*[}][^.{}]*)*
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With