Filter data from a string with regular expression [duplicate]

0

I have a variable that contains the full name of the company and the CNPJ. I'm using

verificaCnpj = re.search("\d{2}.\d{3}.\d{3}/\d{4}-\d{2}", variavel)

to locate the CNPJ (because there are other variables that DO NOT CONTAIN CNPJ, and that should be discarded).

I want to REPLACE this variable so that it will ONLY contain the CNPJ, discarding the company name. What is the best way to do this?

    
asked by anonymous 07.08.2017 / 16:34

1 answer

1

Just call group()

cnpj = re.search("\d{2}.\d{3}.\d{3}/\d{4}-\d{2}", variavel).group()
    
07.08.2017 / 16:43