Replace words in a text that must be formed of letters and numbers

Asked

Viewed 58 times

0

I am trying to replace words in a text that are formed, obligatorily, formed of letters and numbers.

I tried that:

def passwords():

    df['C'] = df['C'].str.replace(r'[a-zA-Z0-9]', '<password>')

    return df

My data:

       A       B                                                  C
   Joana      MG                              minha senha é aaabb123
  Marcos      AM        eu tentei colocar a minha senha varias vezes
   Paulo      RS       eu tenho duas senhas: 321cccppp e r1t2r3t4r5t

My result is horrible:

       A       B                                                            C
   Joana      MG                     <password><password><password><password>
  Marcos      AM <password><password><password><password><password><password>
   Paulo      RS <password><password><password><password><password><password>

Good output:

       A       B                                                  C
   Joana      MG                            minha senha é <password>
  Marcos      AM        eu tentei colocar a minha senha varias vezes
   Paulo      RS       eu tenho duas senhas: <password> e <password>       

1 answer

3


This regex works well here:

([0-9]()|[A-Za-z]())+\2\3

Basically what this regex does is create 2 capture groups, one of numbers([0-9]) and one with uppercase and minuscule letters([a-zA-Z]), and then she checks for the 2 groups in the string(\2 = group 2) and (\3 = group 3).

See working on Repl.it.

Browser other questions tagged

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