Formatting dates (like PHP printf)

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, MudGuard

Post Reply
PeteTheBloke
Posts: 39
Joined: Fri Apr 22, 2005 8:15 am
Location: N. Ireland
Contact:

Formatting dates (like PHP printf)

Post by PeteTheBloke »

Hello

I have a regex which finds dates output from a MS Access database

\t([0-9]+)/([0-9]+)/([0-9]{4})

I want to import it into MySQL so I replace with

\t\3\2\1

But this changes 28/3/2008 into 2008328 where I want 20080328

I've solved the problem by re-exporting from Access, but is there a tidy way I could have added leading zeroes (like I would in PHP by using printf - which, I believe, is an old C function).

Thanks,

Pete
gan

Post by gan »

You could run the search and replace twice like shown below.

Search for:

Code: Select all

([0-9]+)/([0-9])/([0-9]{4})
Replace with:

Code: Select all

\30\2\1
Search for:

Code: Select all

([0-9]+)/([0-9][0-9])/([0-9]{4})
Replace with:

Code: Select all

\3\2\1
If you create macro it can be done with a single click or shortcut key as well. Cannot think of another way of doing this to check if there is 1 or 2 digits and then add a 0 if only 1 digit. Maybe someone else have an idea how to solve that with a single search and replace, but i don't think it can be done.
Last edited by gan on Tue Jun 10, 2008 2:30 pm, edited 1 time in total.
PeteTheBloke
Posts: 39
Joined: Fri Apr 22, 2005 8:15 am
Location: N. Ireland
Contact:

Post by PeteTheBloke »

Thanks. That'll do it. I'll make a macro ready for next time.

Pete
Post Reply