-2

I want to parse month and year from given string.

I have list of strings like below:

a = ['WORK EXPERIENCE: ',
 'Cause + Effect Strategy & Marketing (CESM) | Rochester, New York ',
 '',
 '              May 17-Aug 17 ',
 '',
 '                  Jun 14-Jun 15 ',]

Now I want to first parse date like "May 17-Aug 17" and "Jun 14-Jun 15" from list.

I tried to use for loops on each element of list and used datefinder and parser, so I can get date string, but I got empty list.

I tried to use regex for find date like below:

re.findall(r'((?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*(?:-|\.|\s|,)\s?\d{,2}[a-z]*(?:-|,|\s)?\s?\d{2,4})',string)

I got from above regex.

halfer
  • 19,824
  • 17
  • 99
  • 186
Pmsheth
  • 176
  • 1
  • 13
  • `start,end = "May 17-Jul 18".split("-")` will put those part-strings into your variables. My guess is, thats not what you wanted. Please flesh out your question so its more clear what you did and what you want. Show us your code as [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Patrick Artner Oct 08 '18 at 06:35
  • Thanks for your comment but it will not work all the time parse from text. I guess I need to edit question again – Pmsheth Oct 08 '18 at 06:37
  • Reading this might help you: https://stackoverflow.com/questions/3276180/extracting-date-from-a-string-in-python – jalazbe Oct 08 '18 at 07:00
  • I don't understand this question as it stands - is it now an answer? If so, please roll it back to the last good question state, and put answer material in your answer below. – halfer Oct 08 '18 at 14:53

1 Answers1

0

Initially I want to extract Period of Month and year from string ( ex: May 17-Aug 17). I tried to use Datefinder and Parser library but it was not able to extract it. But later I tried with regex and it gave me answer which I need: ((?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*(?:-|\.|\s|,)\s?\d{,2}[a-z]*(?:-|,|\s)?\s?\d{2,4})' will find month in given regex and then find year.

a = ['WORK EXPERIENCE: ',
     'Cause + Effect Strategy & Marketing (CESM) | Rochester, New York ',
     '',
     '              May 17-Aug 17 ',
     '',
     '                  Jun 14-Jun 15 ',]

date = []
for text in a:

match = re.findall(r'((?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*(?:-|\.|\s|,)\s?\d{,2}[a-z]*(?:-|,|\s)?\s?\d{2,4})',text)
if match:
    date.append(match)
else:
    pass

print(date)
halfer
  • 19,824
  • 17
  • 99
  • 186
Pmsheth
  • 176
  • 1
  • 13