jeudi 8 novembre 2018

Remove everything after regex pattern match but keep pattern

I was searching for a way to remove all characters past a certain pattern match. I know that there are many similar questions here on SO but i was unable to find one that works for me. Basically i have a fixed pattern (\w\w\d\d\d\d), and i want to remove everything after that, but keep the pattern.

ive tried using:

test = 'PP1909dfgdfgd'
done = re.sub ('(\w\w\d\d\d\d/w*)', '\w\w\d\d\d\d/', test)

but still get the same string ..

example:

dirty = 'AA1001dirtydata'
dirty2 = 'AA1001222%^&*'

Desired output:

clean = 'AA1001'

Thanks in advance!=)

Aucun commentaire:

Enregistrer un commentaire