y($$) |> z($$) any random string a() |> b($$) |> c($$)" output -> vec[ "x() |> y($$) |> z($$)", "a() |> b($$) |> c($$)" ] // " /> y($$) |> z($$) any random string a() |> b($$) |> c($$)" output -> vec[ "x() |> y($$) |> z($$)", "a() |> b($$) |> c($$)" ] // " /> y($$) |> z($$) any random string a() |> b($$) |> c($$)" output -> vec[ "x() |> y($$) |> z($$)", "a() |> b($$) |> c($$)" ] // "/>

Regex to match all occurrences of pipe operator (|>) expressions in hacklang

113 views Asked by At

For eg:

Input -> "x() |> y($$) |> z($$) any random string a() |> b($$) |> c($$)"

output -> vec[
           "x() |> y($$) |> z($$)", 
           "a() |> b($$) |> c($$)"
         ] // this contains two groups of expressions.

I'm not sure if this is possible via regex. Can anyone help me with the regex expression if possible? Thanks

1

There are 1 answers

7
sln On BEST ANSWER

I know this could be done other ways but this is most clear to me

Method 1 : - allow single level open-close parenthesis to contain whitespace.
If there is no closing parenthesis it defaults to Method 0 rules.

(?:\(.*?\)|(?!\|>|\s).)*(?:\s*\|>\s*(?:\(.*?\)|(?!\|>|\s).)*)+

https://regex101.com/r/zolgRg/1

 (?:
    \( .*? \) 
  | (?! \| > | \s )
    . 
 )*
 (?:
    \s* \| > \s* 
    (?:
       \( .*? \) 
     | (?! \| > | \s )
       . 
    )*
 )+

Method 0 : - whitespace inside parenthesis are nothing special, just a delimiter.

(?:(?!\|>|\s).)*(?:\s*\|>\s*(?:(?!\|>|\s).)*)+

https://regex101.com/r/pZ01Ki/1

 (?:
    (?! \| > | \s )
    .
 )*
 (?:
    \s* \| > \s* 
    (?:
       (?! \| > | \s )
       .
    )*
 )+