识别前面随机字符的月份

时间:2017-09-02 18:10:02

标签: python regex regex-lookarounds

我希望确定在python中可能有或没有一个随机字符的月份。例如,我想确定:

  1. loctober as october
  2. yaug as aug
  3. 前面的字母不总是l或y,并且比8月和10月更多的月份。

    我已经尝试过识别这几个月:

    odd_months = ['[a-z]jan', '[a-z]january', '[a-z]feb', '[a-z]february', '[a-z]mar', '[a-z]march', 
              '[a-z]apr', '[a-z]april', '[a-z]may', '[a-z]jun', '[a-z]june', '[a-z]jul', 
              '[a-z]july', 'iaug',  '[a-z]august', '[a-z]sep', '[a-z]september', '[a-z]oct', 
              '[a-z]october', '[a-z]nov', '[a-z]november', '[a-z]dec', '[a-z]december']
    

    例如

    'loct' in odd_months #False 
    

4 个答案:

答案 0 :(得分:2)

我会利用calendar模块:

import calendar

names_and_abbrs = calendar.month_name[1:] + calendar.month_abbr[1:]

def isOddMonth(name):
    return (name.title() in names_and_abbrs) or (name[1:].title() in names_and_abbrs)

或者:

def isOddMonth(name):
    return any(n.title() in names_and_abbrs for n in (name, name[1:]))

样品使用:

isOddMonth('aug') == True
isOddMonth('loct') == True
isOddMonth('DECEMBER') == True
isOddMonth('februa') == False
isOddMonth('') == False
isOddMonth('123') == False

答案 1 :(得分:0)

'loct' in odd_months检查odd_months是否包含'loct'。数组中没有这样的字符串,因此返回False

但无论如何我认为使用正则表达式是开销。我建议完全采用另一种方法:

def validate(s):
    months = {
        'jan', 'january', 'feb', 'february', 'mar', 'march', 'apr', 'april',
        'may', 'jun', 'june', 'jul', 'july', 'aug', 'august', 'sep', 'september',
        'oct', 'october', 'nov', 'november', 'dec', 'december'
    }

    if s in months:
        return s

    if s[1:] in months:
        return s[1:]

print(validate('apr')) #=> 'apr'
print(validate('qapr')) #=> 'apr'
print(validate('qqapr')) #=> None

答案 2 :(得分:0)

' [A-Z]扬'是一个字符串而不是正则表达式,使用字符串,如下所示:

odd_months = ['jan', 'january', 'feb', 'february', 'mar', 'march', 
          'apr', 'april', 'may', 'jun', 'june', 'jul', 
          'july', 'aug',  'august', 'sep', 'september', 'oct', 
          'october', 'nov', 'november', 'dec', 'december']

def is_oddmonth(month):
  return any(odm in month.lower() for odm in odd_months)

print(is_oddmonth('lOct')) # True
print(is_oddmonth('yaUg')) # True
print(is_oddmonth('januArysd')) # True
print(is_oddmonth('yammay')) # True
print(is_oddmonth('decimal')) # True
print(is_oddmonth('novel')) # True

print(is_oddmonth('ocr')) # False
print(is_oddmonth('auf')) # False
print(is_oddmonth('jaduary')) # False
print(is_oddmonth('mat')) # False
print(is_oddmonth('nod')) # False
print(is_oddmonth('dek')) # False

答案 3 :(得分:0)

您可以使用dict和正则表达式来执行以下操作:

odd_months={re.compile(r'\w?oct(?:ober)?'): "october", re.compile(r'\w?aug(?:ust)?'): "august"}

for s in ('loct', 'oct', 'loctober', 'yaug', 'waugust', 'nothingburger'):
    for pat, key in odd_months.items():
        if pat.match(s):
            print '"{}"=>{}'.format(s,key)
            break
    else:
        print '"{}" no match'.format(s) 

打印:

"loct"=>october
"oct"=>october
"loctober"=>october
"yaug"=>august
"waugust"=>august
"nothingburger" no match

您还可以使用以下事实:每个月都是唯一的,3个字母的表示形式是唯一的。因此,您可以使用dict同时使用3个字母和全名以及in运算符来测试一个月:

import calendar
def find_name(x):
   months={k.lower():v for k,v in 
            zip(calendar.month_name[1:]+calendar.month_abbr[1:], calendar.month_name[1:]*2)}    
   for k,v in months.items():
       if k in x.lower():
           return v
   else:
      return False