0

I have dates as MonthYear, Month-Year (ex: May16, Jul-21) but I need them to be in dd.mm.yyyy format.

For example:
Aug20, needs to become 01.08.2020
May-19, needs to become 01.05.2019
Jul18, needs to become 01.07.2018
Nov-17, needs to become 01.11.2017

Thank you.

3
  • 2
    This not a job for Npp, you'd better write a script in your favorite scripting language.
    – Toto
    Commented Aug 21, 2022 at 8:14
  • @Toto Isn't it possible with something like this ((Jan)|(Feb)|(Mar)|(Apr)|(May)|(Jun)|(Jul)|(Aug)|(Sep)|(Oct)|(Nov)|(Dec))-(([0-4])\d|[5-9]\d))
    – moninah
    Commented Aug 21, 2022 at 9:07
  • Yes, it can be done with conditional replacement but it is unreadable and unmaintainable
    – Toto
    Commented Aug 21, 2022 at 9:50

2 Answers 2

2

If you really want to do it in a single replacement.

You have to use conditional replacements but it is unreadable and unmaintainable !


  • Ctrl+H
  • Find what: (?:(Jan)|(Feb)|(Mar)|(Apr)|(May)|(Jun)|(Jul)|(Aug)|(Sep)|(Oct)|(Nov)|(Dec))-?(\d\d)
  • Replace with: 01(?1.01.)(?2.02.)(?3.03.)(?4.04.)(?5.05.)(?6.06.)(?7.07.)(?8.08.)(?9.09.)(?10.10.)(?11.11.)(?12.12)20$13
  • TICK Match case
  • TICK Wrap around
  • SELECT Regular expression
  • UNTICK . matches newline
  • Replace all

Explanation:

(?:             # non capture group
(Jan)           # group 1, Jan
|               # OR
(Feb)           # group 2, Feb
|               # OR
(Mar)           etc.    
|
(Dec)           # group 12 Dec
)               # end group
-?              # optional hyphen
(\d\d)          # group 13, 2 digits

Replacement:

01              
(?1.01.)        # if group 1 exists, print .01.
(?2.02.)        # if group 2 exists, print .02.
etc.
(?12.12)        # if group 12 exists, print .12.
20              # 20
$13             # content of group 13, the 2 digit year

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

0
1

Doing it with one regex is horribly complex and is not worth the effort (unless one takes it as a challenge).

You would need to write 12 replacements for each month. For example:

Find what : Aug-?(\d\d)
Replace with : 01.08.20\1
Tick "Regular expression" and untick ". matches newline".

Where -? means the character - is optional.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .