title: Regular expression to group a pattern OR group empty string as ""
body: <p>I'm using Python 3.3.2 with regular expressions. I have a pretty simple function</p>
  
  <pre><code>def DoRegexThings(somestring):
      m = re.match(r'(^\d+)( .*$)?', somestring)
      return m.group(1)
  </code></pre>
  
  <p>Which I am using to just get a numeric portion at the beginning of string, and discard the rest. However, it fails on the case of an empty string, since it is unable to match a group.</p>
  
  <p>I've looked at <a href="https://stackoverflow.com/questions/3333461/regular-expression-which-matches-a-pattern-or-is-an-empty-string">this similar question</a> which was asked previously, and changed my regular expression to this:</p>
  
  <pre><code>(^$)|(^\d+)( .*$)?
  </code></pre>
  
  <p>But it only causes it to return <code>"None"</code> every time, and still fails on empty strings. What I really want is a regular expression which I can use to either grab the numeric portion of my record, e.g. if the record is <code>1234 sometext</code>, I just want <code>1234</code>, or if the string is empty I want <code>m.group(1)</code> to return an empty string. My workaround right now is </p>
  
  <pre><code>m = re.match(r'(^\d+)( .*$)?', somestring)
  if m == None: # Handle empty string case
      return somestring
  else:
      return m.group(1)
  </code></pre>
  
  <p>But if I can avoid checking the match object for <code>None</code>, I'd like to. Is there a way to accomplish this?</p>