0

I need to match dates in form:

January 10, 2012

And replace in form:

10 January 2012

Here is what I have so far:

        pattern = re.findall(r'(?:January|February|March|April|May|June|July|August|September|October|November|December)\s\d\d,\s\d{4}', text)
        print pattern

How do i replace?

4
  • 2
    What have you tried? Please post your code and any specific problems you're having. Commented Apr 25, 2011 at 21:00
  • @Doug T. - Even if it's not, it's still not a passable question. As it stands, the question appears to be "I don't know regular expressions, write one for me." Commented Apr 25, 2011 at 21:01
  • Yes I need help convert from form January 10, 2012 to form 10 January 2012 Commented Apr 25, 2011 at 21:01
  • @michael: "replace" is entirely the wrong concept. You're just formatting the output. Commented Apr 25, 2011 at 21:09

2 Answers 2

5

Use strptime() to parse the date and strftime() to generate the new string. There is no reason to use a regex in this case.

In [1]: import time
In [2]: tm = time.strptime('January 10, 2012', '%B %d, %Y')
In [3]: time.strftime('%d %B %Y', tm)
Out[4]: '10 January 2012'
Sign up to request clarification or add additional context in comments.

Comments

0

Just in general, something like this:

find: ^(January) (\d{1,2}), (\d{4})$ replace $2 $1 $3

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.