Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could capture the number and check the length before adding 0, but you might be able to use this instead:</p> <pre><code>import re txt = 'file8, file9, file10' pat = r"(?&lt;!\d)(\d)(?=,|$)" regexp = re.compile(pat) print(regexp.sub(r"0\1", txt)) </code></pre> <p><a href="http://regex101.com/r/bA3xC6" rel="nofollow">regex101 demo</a></p> <p><code>(?&lt;! ... )</code> 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, <code>(?&lt;!a)b</code> will match all <code>b</code> in a string, except if it has an <code>a</code> before it, meaning <code>bb</code>, <code>cb</code> matches, but <code>ab</code> doesn't match. <code>(?&lt;!\d)(\d)</code> thus matches a digit, unless it has another digit before it.</p> <p><code>(\d)</code> is a single digit, enclosed in a capture group, denoted by simple parentheses. The captured group gets stored in the first capture group.</p> <p><code>(?= ... )</code> is a positive lookahead. This matches <em>only</em> if the pattern inside the positive lookahead matches after the pattern before this positive lookahead. In other words, <code>a(?=b)</code> will match all <code>a</code> in a string only if there's a <code>b</code> after it. <code>ab</code> matches, but <code>ac</code> or <code>aa</code> don't.</p> <p><code>(?=,|$)</code> is a positive lookahead containing <code>,|$</code> meaning either a comma, or the end of the string.</p> <p><code>(?&lt;!\d)(\d)(?=,|$)</code> 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload