0

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

Abhishek
  • 1,302
  • 10
  • 18
  • Do you just need the pipe ? regex like `\|>`. In double quoted string `"\\|>"` – sln Mar 10 '23 at 19:37
  • This won't give me the entire expression "x() |> y($$) |> z($$)". Check the output for the desired result. – Abhishek Mar 10 '23 at 19:39

1 Answers1

0

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 )
       .
    )*
 )+
sln
  • 2,071
  • 1
  • 3
  • 11
  • The regex fails for methods with two arguments. Like `"x() |> y(a=$$, b=5) |> z($$) any random string a() |> b($$) |> c($$)"`. I modified the regex to this `/(?(?:(?![\|>+]+).)*(?:\s[\|>+]+\s*(?:(?![\|>+]+|\s).)*)+)/gm` does this looks good? – Abhishek Mar 11 '23 at 09:34
  • that doesn’t work either :/ – Abhishek Mar 11 '23 at 10:10
  • It fails because you introduced a space in the middle of `(?:(?!\|>|\s ))*` where a space was a delimiter. I can match balanced text and insert that and it would check for paren's and nested paren's. If you use PCRE style or Dot Net, engine no problem. Otherwise, it would be a single open - close paren's block where space is allowed. – sln Mar 12 '23 at 01:55
  • That's right. It's due to the space delimiter. I'm using hacklang to run these regex. The approach that I went with is to remove the space within the parentheses only in the string using this regex `/\(([^)]+)\)/`. Then provided this formatted string to your regex and it worked perfectly fine. is there any better way to deal with this issue? – Abhishek Mar 12 '23 at 14:45
  • "Otherwise, it would be a single open - close paren's block where space is allowed." --> Not sure If I understood this. Can you please give some examples? – Abhishek Mar 12 '23 at 14:49
  • 1
    I put an update method that will parse parenthesis. This is just a mod of the original regex. You don't have to use the extra step now. Method 1 still works either way. – sln Mar 12 '23 at 17:30
  • Hey @sln, I have one last request. Can you please help me out with this? I have posted this question here - https://stackoverflow.com/questions/75734043/extract-the-input-parameters-of-the-a-method-using-regex-into-parameter-and-valu It would be of great help if you can help me out. Thanks – Abhishek Mar 14 '23 at 13:53