In Perl (v5.30), I need to split strings on whitespace, which is not inside matching braces. For example:
"A {B C} D" -> A|{B C}|D
"A{B C}D" -> A{B C}D
"A{ B }C" -> A{ B }C
"AB {C D} EF" -> AB|{C D}|EF
Any ideas?
As long as {
and }
are balanced and there is no escaping you may use this regex for splitting on horizontal whitespaces that are not inside {...}
:
/\h+(?![^{]*})/
RegEx Details:
\h+
: Match 1 or more horizontal whitespaces(?![^{]*})
: Lookahead condition to assert a failure when we have 0 or more of non-{
characters between current position and }