Python pattern searching using re standard lib, str.find() I'm trying to improve some code in order to get a better perfomance, I have to do a lot of pattern matching for little tags on medium large strings, for example:

import re
STR = "0001x10x11716506872xdc23654&amp;xd01371600031832xx10xID=000110011001010\n"

def getID(string):
    result = re.search('.*ID=(\d+)', string)
    if result:
        print result.group(1)
    else:
        print "No Results"


Taking in mind the perfomance I rewrite it:

def getID2(string):
    result = string.find('ID=')
    if result:
        result += 3
        print string[result:result+15]
    else:
        print 'No Results'


Are there a better way to improve these approaches?
Any comments are welcome...
If you name your function getSomething(), I expect it to return a value with no side effects.  I do not expect it to print anything.  Therefore, your function should either return the found string, or None if it is not not found.

Your original and revised code look for different things: the former wants as many digits as possible, and the latter wants 15 characters.  Combining the two requirements, I take it that you want 15 digits.

re.search() lets the regular expression match any part of the string, rather than the entire string.  Therefore, you can omit .* from the beginning of your regular expression.

Here's how I would write it:

import re
STR = "0001x10x11716506872xdc23654&amp;xd01371600031832xx10xID=000110011001010\n"

def getID(string):
    result = re.search('ID=(\d{15})', string)
    return result and result.group(1)

print getID(STR) or "No Results"