0

I have the following string

aaa\bbb\ccc\ddd **16 April**\xyz

The date, in this case 16 April, changes depending on the start time of the event, the format of the rest of the string may change becoming either shorter or longer (below)

aaa\bbb\ccc\ddd\**eee** **16 April**\xyz

I would like to always be able to select '16 April' regardless of the rest of the length of the rest of variable. The date is not always '16 April' but the start date of whatever event I'm being fed by the external program.

I guess I could do

if April in 'aaa\bbb\ccc\ddd\**eee** **16 April**\xyz': 
   print 'success'

But I didn't know if there was a better way...

I need to do this so I can reformat the date to 16-04-2013..

timss
  • 9,982
  • 4
  • 34
  • 56
DavidJB
  • 2,272
  • 12
  • 30
  • 37

2 Answers2

2

This should do it.

import datetime
import re

# Note the \\x to escape \x
foo = "aaa\bbb\ccc\ddd **16 April**\\xyz"
bar = "aaa\bbb\ccc\ddd\**eee** **1 December**\\xyz"

# Could do \d+ and \w+ aswell, it doesn't seem to matter too much in this situation
pattern = '\*\*(\d{1,2} \w{4,9})\*\*'

# "16 April" etc
foo_format = re.search(pattern, foo).group(1)
bar_format = re.search(pattern, bar).group(1)

year = str(datetime.datetime.now().year)

# Datetime object
foo_date = datetime.datetime.strptime(year + " " + foo_format, "%Y %d %B")
bar_date = datetime.datetime.strptime(year + " " + bar_format, "%Y %d %B")

print foo_date.strftime("%Y-%m-%d")
print bar_date.strftime("%Y-%m-%d")

Read more about \x in Why is '\x' invalid in Python?

Community
  • 1
  • 1
timss
  • 9,982
  • 4
  • 34
  • 56
0

You can utilize regular expressions:

import re

a = re.search("16 April", "sample string")
print a.group(0)

The last statement will either return "16 April" if it is in the string or None

gnerkus
  • 11,357
  • 6
  • 47
  • 71
  • Not flexible enough. Should handle any date of the format OP has given, and not have to explicitly have to search for the date. – timss Apr 18 '13 at 17:58
  • I just gave this as an example. The first argument ought to be a regular expression. – gnerkus Apr 18 '13 at 18:07