noob noob - 4 months ago 12
Python Question

adding spaces inside string using regex sub

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?

Answer

You may use re.findall also,

>>> s = "123456789"
>>> ' '.join(re.findall(r'..|.', s))
'12 34 56 78 9'
>>> 

r'..|.' regex matches two chars or a single character (first preference goes to .. and then .)