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..

3
  • 1
    You could use regex to accomplish such, but the simple python String methods (implicitly used here) are often faster and avoid an unnecessary import. Apr 18, 2013 at 17:23
  • you could use regex, to get the date part, and after that, check if the value is of type date
    – eLRuLL
    Apr 18, 2013 at 17:23
  • Is it some sort of `\`-delimeted csv?
    – J0HN
    Apr 18, 2013 at 17:29

2 Answers 2

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?

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

2
  • 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, 2013 at 17:58
  • I just gave this as an example. The first argument ought to be a regular expression.
    – gnerkus
    Apr 18, 2013 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.