Search code examples
regexperl

Split on spaces which are not inside matching braces


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?


Solution

  • 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 Demo

    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 }