1

I'm using Python 3.3.2 with regular expressions. I have a pretty simple function

def DoRegexThings(somestring):
    m = re.match(r'(^\d+)( .*$)?', somestring)
    return m.group(1)

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.

I've looked at this similar question which was asked previously, and changed my regular expression to this:

(^$)|(^\d+)( .*$)?

But it only causes it to return "None" 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 1234 sometext, I just want 1234, or if the string is empty I want m.group(1) to return an empty string. My workaround right now is

m = re.match(r'(^\d+)( .*$)?', somestring)
if m == None: # Handle empty string case
    return somestring
else:
    return m.group(1)

But if I can avoid checking the match object for None, I'd like to. Is there a way to accomplish this?

Community
  • 1
  • 1
Dizzyspiral
  • 745
  • 6
  • 12
  • `(^$)|(^\d+)( .*$)?` will "work", even for empty strings, but of course the number (if matched) will now be in group 2 instead of group 1 because you've added another capturing group (that will match the empty string) before it. – Tim Pietzcker May 02 '14 at 16:55
  • Also, *if* you need to compare to `None`, use `if m is None:` instead of `if m == None:`, but in this case, `if not m:` would be the preferred solution - which you don't need, however (see my answer). – Tim Pietzcker May 02 '14 at 16:57

1 Answers1

1

I think you're making this overly complicated:

re.match(r"\d*", somestring).group()

will return a number if it's at the start of the string (.match() ensures this) or the empty string if there is no number.

>>> import re
>>> somestring = "987kjh"
>>> re.match(r"\d*", somestring).group()
'987'
>>> somestring = "kjh"
>>> re.match(r"\d*", somestring).group()
''
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561