title: re.sub match with first occurrence of bracketed characters
body: <p>I'm trying to capture the first group of characters before one or more underscores or dashes in a string using <code>re.sub</code> in Python 3.7. My current function is:
   <code>re.sub(r'(\w+)[-_]?.*', r'\1', x)</code>.</p>
  
  <p>Example strings:</p>
  
  <pre><code>x = 'CAM14_20190417121301_000'
  x = 'CAM16-20190417121301_000'
  </code></pre>
  
  <p>Actual output:  </p>
  
  <pre><code>CAM14_20190417121301_000
  CAM16
  </code></pre>
  
  <p>Desired output:</p>
  
  <pre><code>CAM14
  CAM16
  </code></pre>
  
  <p>Why is it working when there is a dash after the first group, but not an underscore? I also tried <code>re.sub(r'(\w+)_?.*', r'\1', x)</code> to try and force it to catch the underscore, but that returned the same result. I would like the code to be flexible enough to catch either.</p>