perl - Regex to capture everything inside ( ... ) but also handle cases where ( is used inside -


i trying capture using perl regex data found inside this:

variable_myname(variable_data); 

so used:

variable_([a-za-z_]+)(\s+)?\((.*?)\) 

this allowed me capture myname of variable (which prefixed variable_) data inside (...).

however, doesn't work if user uses (allowed) syntax of:

variable_oneexp("this value ( ... ) "); 

which because of ", ( , ) should ignored.

same behavior should handled if ' used:

variable_twoexp('this value ( ... ) '); 

finally, behavior should supported:

variable_threeexp('this value ' + ' string '); 

though, don't think last example makes difference regex.

some pointers/assistance appreciated.

you use negated class instead of lazy .*?, use alternation match between single/double quotes:

variable_([a-za-z_]+)\s*\(((?:'[^']+'|"[^"]+"|[^()])+)\) 

regex101 demo

i removed capture group around \s+, , turned \s* since don't believe need spaces in capture group. revert if need it.


Comments

Popular posts from this blog

css - I want to align grid in center -

Contact Form PHP Email Script -

python - SWIG function not printing output -