Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and replace with reordered date format in notepad++

I have a file with a few thousand rows to be added to a MySQL database. There are date values in the rows which are in the dd-mm-yyyy format but I need them to be in the yyyy-mm-dd format.

E.g., '11-04-2010', needs to become '2010-04-11', in every row.

Is there a simple way to do this in notepad++ or another text editor?

like image 674
Chris J Avatar asked Dec 02 '10 02:12

Chris J


People also ask

How do I change the date format in notepad?

Used Notepad++ to change mm/dd/yyyy to yyyy/mm/dd in several lines of a text file. Script was saved as a macro for next file. Show activity on this post. "(\d{4})-(\d{1,2})-(\d{1,2})T([0-2]\d):([0-5]\d):([0-5]\d)(?:.

How do you use Find and Replace on notepad?

Open the text file in Notepad. Click Edit on the menu bar, then select Replace in the Edit menu. Once in the Search and Replace window, enter the text you want to find and the text you want to use as a replacement. See our using search and replace and advanced options section for further information and help.

How do I find and replace a pattern in Notepad++?

Using Regex to find and replace text in Notepad++ In all examples, use select Find and Replace (Ctrl + H) to replace all the matches with the desired string or (no string). And also ensure the 'Regular expression' radio button is set.

How do I find and replace end of line in Notepad++?

In Notepad++, press Ctrl-H (Replace...). In the Replace dialog box, click Extended under Search Mode. In the "Find what" box type !\ r and in the "Replace with" box type \r.


2 Answers

You can do this with Textpad:

Find: ([0-9]+)-+([0-9]+)-+([0-9]+)

Replace: \3-\2-\1

like image 65
crnlx Avatar answered Sep 21 '22 15:09

crnlx


To make sure you only reorder wrong formats (in case you have mixed formats from merging databases), use this:

([0-9]{2})-+([0-9]{2})-+([0-9]{4}) 

This searches for (four digits, dash, two digits, dash, two digits).

In an regex capable editor like notepad++, replace it with this:

\3-\2-\1 

In a tool like libre office, you need to replace it with this:

$3-$2-$1  

Edit: I wrote a blogpost about this as it seems to be a common problem: http://bytethinker.com/blog/correct-date-format-with-notepad-and-regex

like image 29
Guido Gallenkamp Avatar answered Sep 19 '22 15:09

Guido Gallenkamp