Home > Software engineering >  Empty parenthesis regex
Empty parenthesis regex

Time:08-11

I have a list

list1 = ['main()','("%d",b);']

I just want empty parenthesis which is main() in output. But re.compile only showing (%d) in output

regex1 = re.compile("[()]")
newlist1 = list(filter(regex1.match, list1))
print(newlist1)

Output:

['("%d",b);']

I want:

['main()']

CodePudding user response:

Use

import re
list1 = ['main()','("%d",b);']
regex1 = re.compile(r"\(\)")
newlist1 = list(filter(regex1.search, list1))
print(newlist1)

See Python proof.

  • Related