Carsten Haese wrote: > On Wed, 2007-09-26 at 12:49 -0400, Steve Holden wrote: >> james_027 wrote: >>> hi, >>> >>> how do I regex that could check on any of the value that match any one >>> of these ... 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', >>> 'sep', 'oct', 'nov', 'dec' >>> >>> Thanks >> >>> patr = re.compile('jan|feb|mar|apr|may|jun|jul|aug|sep|nov|oct|dec') >> >>> patr.match("jul") >> <_sre.SRE_Match object at 0x7ff28ad8> >> >>> patr.match("nosuch") > > Unfortunately, that also matches margarine, mayonnaise, and octopus, > just to name a few ;-)
(and so does the solution you sent before :) This is fine IMO since the OP didn't specify the opposite. BTW in my previous post I included an example that ensures that the search month matches the beginning of a word. That was based in that maybe he wanted to match e.g. "dec" against "December" (BTW, it should have been r'\b(?:Jan|Feb|...)' instead). To always match a whole word, a trailing \b can be added to the pattern OR (much better) if the month can appear both in its abbreviated and full form, he can use the extensive set as follows (I hope this is clear, excuse my Thunderbird...): >>> pattern = r"\b(?:%s)\b" % '|'.join(calendar.month_name[1:13] + calendar.month_abbr[1:13]) >>> pattern '\\b(?:January|February|March|April|May|June|July|August|September|October|November|December|Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\\b' >>> target = "Unlike Julia, I like apricots with mayo in august or sep" >>> target 'Unlike Julia, I like apricots with mayo in august or sep' >>> re.findall(pattern, target, re.IGNORECASE) ['august', 'sep'] >>> re.search(pattern, target, re.IGNORECASE) <_sre.SRE_Match object at 0xb7ced640> >>> re.findall(pattern, target, re.IGNORECASE) ['august', 'sep'] Regards, Pablo -- http://mail.python.org/mailman/listinfo/python-list