I did some python-timit or these and translate is 3 times as fast, but I would expect that since in machine code on x86 would be something like
mov ecx,length_of_source mov esi,offset source mov ebx,offset table mov edi,offset dest here: lodsb xlat stosb loop here ; even my assembly is getting rusty ; It is like riding a bike, but I think the bike is rusty too.
That is like 12 clock cycles ( wild guess ) or about 100 mb translated per second. So it would be faster than regex, but it is surprising that in an interpreted language like Python, that a machine like regex would only be 3 time slower.
r='go#get$some&stuff_(from)_#the@web*with:junk;in,it%ok?or[not][eh?' table = string.maketrans("\\$&()@*:;#%?\[\],","_________________") rr= string.translate(r,table) rs= r.translate(table) #Same thing print rr This works too rx = re.sub(r'[^\w\s]|_','-',r) x=re.compile(r'[\$&()@*:;#%?\[\],]') rr=x.sub('_',r)
If a person looked at that without regex experience, it would sure look scary. My first exposure to regex made me wonder about my sanity. Now I can read it just like русский or the bloody queen's english. It used to worry me to stuff like that in a program because I wasn't sure WTF it would do, but now it is no different than anything else, it all becomes NAND in the end.