Python Regex Newbie- matching substrings

Thomas Heller theller at python.net
Thu Jun 12 04:35:43 EDT 2003


danieljng at yahoo.com (Daniel) writes:

> Hi,
> 
> I would like my regex to detect that a month (any month) is in the
> following line, returning the index of the start of the match:
> 
> Jun 05 14:40:26 2003 .....
> 
> 
> So far, I've tried:
> 
>   months       = re.compile('(Jan)')
>   searchResult = months.search('abcJan')
>   print searchResult.start()
> 
> 
> -Which works for just 'Jan' of course, and it correctly prints '3'. 
> 
> 
> How do I get it to work for all months? 
> 
> 
> 
> I tried:
> 
>   months       = re.compile('[(Jan)(Feb)(Mar)]+')
> 
> 
> -but that regex is too 'greedy' ie. matches too easily- I think it
> matches the individual letters in each of Jan, Feb, Mar instead.

IMO you should use
  months = re.compile("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec")
instead.

Thomas




More information about the Python-list mailing list