EP_Guy
EP_Guy

Reputation: 95

Python re.sub not returning expected string of int

I would like to left pad zeros to the number in a string. For example, the string

hello120_c

padded to 5 digits should become

hello00120_c

I would like to use re.sub to make the replacement. Here is my code:

>>> re.sub('(\d+)', r'\1'.zfill(5), 'hello120_c')

which returns

>>> 'hello000120_c'

which has 6 digits rather than 5. Checking '120'.zfill(5) alone gives '00120'. Also, re.findall appears to confirm the regular expression is matching the full '120'.

What is causing re.sub to act differently?

Upvotes: 5

Views: 455

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626896

You cannot use the backreference directly. Use a lamda:

re.sub(r'\d+', lambda x: x.group(0).zfill(5), 'hello120_c')
# => hello00120_c

Also, note that you do not need a capturing group since you can access the matched value via .group(0). Also, note the r'...' (raw string literal) used to declare the regex.

See IDEONE demo:

import re
res = re.sub(r'\d+', lambda x: x.group(0).zfill(5), 'hello120_c')
print(res)

Upvotes: 3

Related Questions