using target words from arrays in regex, pythons version of perls 'map'

Lance Hoffmeyer lance at augustmail.com
Mon May 15 20:41:39 EDT 2006


Hey all, in perl I was able to use the same regular expression multiple times changing one part of it
via a previously defined array and put the results into a new array

IN PERL:

my @targets = ('OVERALL RATING',
               'CLOTHING', '
		ITEMS',
               'ACCESSORIES',
               'SHOES',
               'FINE JEWELRY');

my @JA13 = map {
    $file2 =~/$_.*?(?:(\d{1,3}\.\d)\s+){3}/s;
		} @targets;



So, in python instead of

match2 = re.search('OVEWRALL RATING.*?(?:(\d{1,3}\.\d)\s+){3} ', file2);m01 = match2.group(1) ;print m01
match2 = re.search('CLOTHING.*?(?:(\d{1,3}\.\d)\s+){3} ', file2);       m02 = match2.group(1) ;print m02
match2 = re.search('ITEMS.*?(?:(\d{1,3}\.\d)\s+){3} ', file2);          m03 = match2.group(1) ;print m03
match2 = re.search('ACCESSORIES.*?(?:(\d{1,3}\.\d)\s+){3} ', file2);    m04 = match2.group(1) ;print m04
match2 = re.search('SHOES.*?(?:(\d{1,3}\.\d)\s+){3} ', file2);          m05 = match2.group(1) ;print m05
match2 = re.search('FINE JEWELRY.*?(?:(\d{1,3}\.\d)\s+){3} ', file2);   m06 = match2.group(1) ;print m06


I would have something similar to perl above:


targets = ['OVERALL RATING',
           'CLOTHING', ITEMS',
           'ACCESSORIES',
           'SHOES',
           'FINE JEWELRY']

PROPOSED CODE:

match2 = re.search(targets[i].*?(?:(\d{1,3}\.\d)\s+){3} ', file2);m[i] = match2.group(1)




Lance



More information about the Python-list mailing list