Search code examples
regexpython-3.xply

regex to match the argument of specific functions


how to write a regular expression to match the below string :

name(abc1) or number(param9) or listget(12jtxgh)

I want to match the string enclosed in brackets only if it is prepended by name or number or listget.

I tried to this : r'((.*?))'

and if my expression looks like below :

(name(foo) & number(bar)) - listget(baz)

then it starts matching (name(foo) also. I want my regex to extract only foo, bar and baz from the above expression as it is appended by name, number, listget.

I have to write regex in this form only #r'regex'


Solution

  • The following regular expression should do the trick:

    (?:listget|name|number)\(([^)]+)\)
    

    You can try a working demo by visiting this link. As others pointed out, parenthesis must be escaped in order to match their literal, otherwise they are being used by the regex for different purposes (like capturing groups).