trying to match a string

Andrew Freeman alif016 at
Sat Jul 19 21:00:52 CEST 2008

Andrew Freeman wrote:
> John Machin wrote:
>> A couple of points:
>> (1) Instead of search(r'^blahblah', ...) use match(r'blahblah', ...)
>> (2) You need to choose your end-anchor correctly; your pattern is
>> permitting a newline at the end:
I forgot to change search to match. This should be better:

def match(var):
    if re.match(r'[LRM]*\Z', var):
        return True
        return False

I was also thinking if you had a list of these items needing to be 
verified you could use this:
 >>> l = ['LLMMRR', '00thLL', 'L', '\n']
 >>> out = []
 >>> map(lambda i: match(i)==False or out.append(i), l)
 >>> print out
['LLMMRR', 'L']


More information about the Python-list mailing list