Home > Software design >  Regular expression to differentiate function call and variable declaration in custom language
Regular expression to differentiate function call and variable declaration in custom language

Time:10-24

I'm trying to build regex to differentiate variable declaration and function calls in custom language.

Variable declaration looks like this:

let var1(string)
let var2 (string)
let var3,var4,var5(string)

Function call looks like this:

func1(1,2..)
func2()
->func3()

Regex I used for the function call is

(?i:(?![let])\s*(\b[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*?\b)(?=\(\)?))

This regex matches variable declaration as well. If there is let keyword in front of the variable I want to exclude it in this regex.

Regex for the variable match is here:

(?i:((?<=(\\ |=|-|\\&|\\\\|/|<|>|\\(|,))\\s*\\b([a-zA-Z_xf-xff][a-zA-Z0-9_xf-xff]*?)\\b(?!(\\(|\\.))|\\b([a-zA-Z_xf-xff][a-zA-Z0-9_xf-xff]*?)\\b(?=\\s*(\\.|\\ |=|-|\\&|\\\\|/|<|>|\\(|\\)))))

This regex matches let var1 (String) but not let var1(String)

I already spent enough time trying to figure out this :( . Your help is highly appreciated.

CodePudding user response:

Below regex for the function call solved the issue. It's able to differentiate variable vs function call.

(?i:(?<=(\.|\>|=))\s*(\b[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*?\b)(?=\(\)?))

Thanks to regex101.com

  • Related