You could capture the number and check the length before adding 0, but you might be able to use this instead:
import re
txt = 'file8, file9, file10'
pat = r"(?<!\d)(\d)(?=,|$)"
regexp = re.compile(pat)
print(regexp.sub(r"0\1", txt))
(?<! ... )
is called a negative lookbehind. This prevents (negative) a match if the pattern after it has the pattern in the negative lookbehind matches. For example, (?<!a)b
will match all b
in a string, except if it has an a
before it, meaning bb
, cb
matches, but ab
doesn't match. (?<!\d)(\d)
thus matches a digit, unless it has another digit before it.
(\d)
is a single digit, enclosed in a capture group, denoted by simple parentheses. The captured group gets stored in the first capture group.
(?= ... )
is a positive lookahead. This matches only if the pattern inside the positive lookahead matches after the pattern before this positive lookahead. In other words, a(?=b)
will match all a
in a string only if there's a b
after it. ab
matches, but ac
or aa
don't.
(?=,|$)
is a positive lookahead containing ,|$
meaning either a comma, or the end of the string.
(?<!\d)(\d)(?=,|$)
thus matches any digit, as long as there's no digit before it and there's a comma after it, or if that digit is at the end of the string.