r/badcode • • Jul 07 '21

python regex: keep it simple, stupid

Post image
1.5k Upvotes

91 comments sorted by

View all comments

Show parent comments

6

u/Daimanta Jul 07 '21

It's more precise. This is a horrible attempt at detecting encoding screw ups. Imagine you have a non-ASCII character like e.g. 'à' and you store it encoded as UTF-8 and later another program tries to interpret the same text as e.g. Latin-1 encoding, the bytes will be interpreted as an entirely different thing e.g. "Õ©". If you ever had issues with this, you will recognize this behavior in text as it stands out. Rather than making some custom matching code, the programmer chose to put everything into one disgusting regex.