Search code examples
regexdatetimerenamefilenamesnano

How to add dots between every two digits of a six digit substring?


I have a text file that contains roughly a thousand file names that I need to change slightly. Each file name is a date and time formatted like:

2013-05-01 120125.jpg

I need to convert all of them to:

2013-05-01 12.01.25.jpg

I'm assuming this would be fairly trivial with regular expressions, but I am always confounded when I try to do anything with them! Help is appreciated!


Solution

  • You want to use a replace technique (in whatever language/environment you are using) on your substrings by capturing like this:

    (\d{2})(\d{2})(\d{2})
    

    *note the curly brackets are for improved efficiency.

    And replace with:

    $1.$2.$3
    

    Here is a demo link.

    Here is a SO page discussing the execution of replacements on nano.