Filter data from a string with regular expression

Asked

Viewed 407 times

0

I have a variable that contains the full company name and CNPJ. I am 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 should be discarded).

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

1 answer

1


Just call the group()

cnpj = re.search("\d{2}.\d{3}.\d{3}/\d{4}-\d{2}", variavel).group()
  • 1

    Before the call of group it will be necessary to check if the text has the CNPJ, otherwise the function search will return None.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.