I have a string I want to split into 2-digit pieces. I tried using regex like so:
import re
s = "123456789"
t = re.sub('..', ".. ", s)
print(t)
I expected to get 12 34 56 78 9 but instead I got '.. .. .. .. 9'. The 9 does not bother me, because I know I will have an even number of digits, but how can I tell the re.sub to not replace the actual digit with a dot?
using python shell 3.5.1
EDIT
checked all 3 answers, and they all work, but the findall seems to be faster (and more elegant IMO ;p ):
import time
import re
s = "43256711233214432"
i = 10000
start = time.time()
while i:
i -= 1
re.sub('(..)', r"\1 ", s)
end = time.time()
elapsed = end - start
print("using r\"\\1 \" : ", elapsed)
i = 10000
start = time.time()
while i:
re.sub('..', r"\g<0> ", s)
i -= 1
end = time.time()
elapsed = end - start
print("using r\"\g<0> \" : ", elapsed)
i = 10000
start = time.time()
while i:
' '.join(re.findall(r'..|.', s))
i -= 1
end = time.time()
elapsed = end - start
print("using findall : ", elapsed)
using r"\1 " : 0.25461769104003906
using r"\g<0> " : 0.09374403953552246
using findall : 0.015610456466674805
2nd EDIT: is there a better way (or any way...) doing this without regex?
\1and\g<0>do pretty much the same and should be equally fast. Using IPython's%timeitfunction gives me 8.5µs for bothsubapproaches, 2.0µs forfindalland 1.5µs forrange(0, len, 2)\g, then\1?findallis fastest (exceptrange), no arguing about that. BTW, when I run your script it's more closely to%timeit, with\gand\1coming out nearly identically.