title: Conditional regular expression in Python
body: <p>I am currently trying to match the following using a regular expression:</p>
  <pre><code>label      string
    a        disvt
    b        disv2
    c        disv1
    d        disv1f
    e        disv10
    f        disr1
  </code></pre>
  <p>I am trying to capture vt, v2, v1 and v10, using a conditional regular expression to capture the last 3 in that list.</p>
  <p>The regular expression I have so far captures vt and v10 but omits v1 and v2. I'm not entirely sure how to create a regex that captures the first digit if there is only one digit after the 'v' and the first two digits if there are two digits after the 'v'</p>
  <p>What I have below captures v10 and vt, which I want, however it omits v2 and v1.</p>
  <pre><code>desired_df['new_column'] = df[&quot;string&quot;].str.extract(pat=&quot;(r\d{1}|vt|v\d{1}?\d(?(1)\w))&quot;, expand=False)
  </code></pre>
  <p>Desired output would have</p>
  <pre><code>desired_df['new_column'].value_counts()
  
  r1  1
  vt  1 
  v10 1
  v1  1
  v2  1
  </code></pre>
  <p>Final new dataframe:</p>
  <pre><code>    label     string    new_column
       a        disvt            vt
       b        disv2           NaN
       c        disv1           NaN
       d        disv1f          NaN
       e        disv10          v10
       f       disr1            r1
  </code></pre>