Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save NHDaly/c59b6eac196a5aa50f43ca7a9e0e056a to your computer and use it in GitHub Desktop.
Save NHDaly/c59b6eac196a5aa50f43ca7a9e0e056a to your computer and use it in GitHub Desktop.
Regex: remove parens to turn a julia function call into a macro call
julia> m = "call( a( x()-1 ), 2+2 )"
>> "call( a( x()-1 ), 2+2 )"
julia> replace(m, r"a(\(((?>[^()]|(?1))*)\))" => s"@b \2")
>> "call( @b x()-1 , 2+2 )"
julia> # You can see the two capture groups here: the first around the parens (used for recursion), and the second inside the parens (for substitution).
julia> match(r"a(\(((?>[^()]|(?1))*)\))", m)
>> RegexMatch("a( x()-1 )", 1="( x()-1 )", 2=" x()-1 ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment