How can I autogenerate a case insensitive REGEXP for a string that may have the case following variations: 'ENTI', 'Enti'
I got this so far but it looks clumsy,
entity_type = 'Enti'
prefix = 'E'
for char in entity_type[1:]:
logs_prefix += '[' + char.upper() + char.lower() + ']'
print logs_prefix
>>>'E[Nn][Tt][Yy]'
My goal is to initially discover the list of active logs (the rotated ones will be ending with a timestamp) which start with a given case insensitive sequence, so I can
regexp_filters = logs_prefix + '_A.out'
command = "ssh %(user)s@%(ip)s 'cd %(source_path)s; ls -t %(regexp_filters)s'" % locals()
and generate different rsync expressions for a range of hosts. Ignore the exclude, its made of several --excludes witholder,开发者_Go百科 crashed '*_A.out' logs
sync = "rsync -e ssh -a %(remote_rsync_binary)s --compress=9 -pgtov %(excluded_expression)s %(filters_expression)s --exclude='*' %s(user)@%(ip)s:%(source_path)s%(file_filter)s %(target_path)s" % locals()
Minor question, how to easily enclose a string/char with other chars, in this case: [char]
EDIT : Found a cleaner solution, is there a better way?
for char in entity_type[1:]:
prefix += "[%s]" % "%s%s" % (char.upper(), char.lower() )
EDIT2: (improvement) as @eyquem wrote,
prefix + ''.join( "[%s%s]" % (char.upper(), char.lower() for char in entity_type[1:])
Try this:
reEnti = re.compile('ent[iy]',re.IGNORECASE)
You could try this (Python 2.7.1):
>>> def goofy_regexp(s):
return '{}|{}'.format(s.upper(), s.title())
>>> goofy_regexp('enti')
'ENTI|Enti'
Here's a 2.6 version with old style string formatting:
>>> def goofy_regexp26(s):
return '%s|%s' % (s.upper(), s.title())
>>> goofy_regexp26('enti')
'ENTI|Enti'
I can't believe python doesn't actually support ignorecase in a regex. Just passing "Ignorecase" as a flag to the regex parser is no better than doing upper() after the fact and is useless for defining a case-insenstive regex
精彩评论