How to change date and month into word

A swapping-ground for Regular Expression syntax

How to change date and month into word

Postby lalajee » Wed Jun 23, 2021 11:14 am

Hi, I have lots of files with following format

word word 21 06 23 word word word
word word 21.06.23 word word word
word word 21-06-23 word word word

I would like to change above file name into "word word 2021 Jun 23 word word word"

I try this
Regex (1)
Match: (.*)(\d\d)( \d\d) (\d\d)(.*)?
Replace: \120\2\3 \4\5

This works for date but how do I change the month from number to word
lalajee
 
Posts: 8
Joined: Wed Jun 23, 2021 11:09 am

Re: How to change date and month into word

Postby therube » Wed Jun 23, 2021 4:40 pm

(JavaScript renaming should be able to do it, I would think.)


Otherwise, do it in steps.

maybe something like this {pseudo-code} as a start:

Match: \s21[\s.-]\d\d[\s.-]\d\d\s

make your year four digits.
maybe change the separator to something potentially unique (that you can match on)
say ^
so then you'd have 2021^01^17
then you could either simply 3:Replace, 2021^01^ With 2021{sp}Jan{sp} (doing that for 01..12)
or you could use the Rename Pairs feature to do them all at once...
therube
 
Posts: 1022
Joined: Mon Jan 18, 2016 6:23 pm

Re: How to change date and month into word

Postby Luuk » Wed Jun 23, 2021 8:16 pm

Greetings Reza. Im thinking probably the easiest way, is to rename your files just like therube is explaining.
There is also a "v2" checkbox to grant many expressions that conducts everything, but its more complicated.
I could give example, but Im not understanding what the other dates or month-names should look like?
Luuk
 
Posts: 320
Joined: Fri Feb 21, 2020 10:58 pm

Re: How to change date and month into word

Postby lalajee » Wed Jun 23, 2021 8:59 pm

I have files from different year and month

word word 17 6 23 word
word word 16 06 23 word
word word 19 05 23 word

Should be like

word word 2017 Jun 23 word
word word 2016 Jun 23 word
word word 2019 May 23 word
lalajee
 
Posts: 8
Joined: Wed Jun 23, 2021 11:09 am

Re: How to change date and month into word

Postby lalajee » Wed Jun 23, 2021 9:05 pm

therube wrote:(JavaScript renaming should be able to do it, I would think.)


Otherwise, do it in steps.

maybe something like this {pseudo-code} as a start:

Match: \s21[\s.-]\d\d[\s.-]\d\d\s

make your year four digits.
maybe change the separator to something potentially unique (that you can match on)
say ^
so then you'd have 2021^01^17
then you could either simply 3:Replace, 2021^01^ With 2021{sp}Jan{sp} (doing that for 01..12)
or you could use the Rename Pairs feature to do them all at once...



I dont know how to use java or write code in java. Can you please help
lalajee
 
Posts: 8
Joined: Wed Jun 23, 2021 11:09 am

Re: How to change date and month into word

Postby Luuk » Thu Jun 24, 2021 1:21 am

This a "v2" regex for 'yy mm dd' formatted dates, and both sides of the date must be space-separated away from words...
If 'yy' starts with 0-2, the first regex converts yy ==> 20yy, if it starts with 3-9, the second regex converts yy ==> 19yy.
Both of the first two regexs also insert the ":" character to make a unique separator for the months, like therube is describing.
All of the other regexs just replace ':MonthNumber' ===> 'MonthAbbreviation'

(.+ )([0-2]\d )([01]\d [123]\d .+)(?X)(.+ )([3-9]\d )([01]\d [123]\d .+)(?X):01(?X):02(?X):03(?X):04(?X):05(?X):06(?X):07(?X):08(?X):09(?X):10(?X):11(?X):12
${1}20$2:$3(?X)${1}19$2:$3(?X)Jan(?X)Feb(?X)Mar(?X)Apr(?X)Mai(?X)Jun(?X)Jul(?X)Aug(?X)Sep(?X)Okt(?X)Nov(?X)Dez


If needing to make modifications, remember that (?X) separates each Match and Replace, so if wanting to make "words" optional, it can be like...
(.*)([0-2]\d )([01]\d [123]\d.*)(?X)(.*)([3-9]\d )([01]\d [123]\d.*)(?X):01(?X):02(?X):03(?X):04(?X):05(?X):06(?X):07(?X):08(?X):09(?X):10(?X):11(?X):12
${1}20$2:$3(?X)${1}19$2:$3(?X)Jan(?X)Feb(?X)Mar(?X)Apr(?X)Mai(?X)Jun(?X)Jul(?X)Aug(?X)Sep(?X)Okt(?X)Nov(?X)Dez

The javascript does also probably conduct all of this to be much easier, but Im not expert with the javascript code.
So it would probably take me much longer to discover the javascript twin that conducts like this "v2" regex.
Luuk
 
Posts: 320
Joined: Fri Feb 21, 2020 10:58 pm

Re: How to change date and month into word

Postby Luuk » Thu Jun 24, 2021 2:04 am

Im just noticed that your last example did also include a one-digit month, so "6" ===> "Jun".
So to fix these, just add one expression at the very beginning to insert "0" before 1-digit months, so like...

If date should always be space-separated away from words ...
(.+ [0-2]\d )(\d [123]\d .+)(?X)(.+ )([0-2]\d )([01]\d [123]\d .+)(?X)(.+ )([3-9]\d )([01]\d [123]\d .+)(?X):01(?X):02(?X):03(?X):04(?X):05(?X):06(?X):07(?X):08(?X):09(?X):10(?X):11(?X):12

${1}0$2(?X)${1}20$2:$3(?X)${1}19$2:$3(?X)Jan(?X)Feb(?X)Mar(?X)Apr(?X)Mai(?X)Jun(?X)Jul(?X)Aug(?X)Sep(?X)Okt(?X)Nov(?X)Dez

If "words" should be optional ...
(.*[0-2]\d )(\d [123]\d.*)(?X)(.*)([0-2]\d )([01]\d [123]\d.*)(?X)(.*)([3-9]\d )([01]\d [123]\d.*)(?X):01(?X):02(?X):03(?X):04(?X):05(?X):06(?X):07(?X):08(?X):09(?X):10(?X):11(?X):12

${1}0$2(?X)${1}20$2:$3(?X)${1}19$2:$3(?X)Jan(?X)Feb(?X)Mar(?X)Apr(?X)Mai(?X)Jun(?X)Jul(?X)Aug(?X)Sep(?X)Okt(?X)Nov(?X)Dez
Luuk
 
Posts: 320
Joined: Fri Feb 21, 2020 10:58 pm

Re: How to change date and month into word

Postby lalajee » Thu Jun 24, 2021 6:29 pm

Thank you for the code but when I try the code i dont see it being change


in the Match field I enter:
(.+ [0-2]\d )(\d [123]\d .+)(?X)(.+ )([0-2]\d )([01]\d [123]\d .+)(?X)(.+ )([3-9]\d )([01]\d [123]\d .+)(?X):01(?X):02(?X):03(?X):04(?X):05(?X):06(?X):07(?X):08(?X):09(?X):10(?X):11(?X):12
${1}0$2(?X)${1}20$2:$3(?X)${1}19$2:$3(?X)Jan(?X)Feb(?X)Mar(?X)Apr(?X)Mai(?X)Jun(?X)Jul(?X)Aug(?X)Sep(?X)Okt(?X)Nov(?X)Dez

in the Replace field: \120\2\3 \4\5

It changes the year but not the month and also its putting extra space at the end of file name
lalajee
 
Posts: 8
Joined: Wed Jun 23, 2021 11:09 am

Month Number into Month Abbreviation

Postby Luuk » Thu Jun 24, 2021 8:36 pm

Line1 is 15-matches, Line2 is 15-replaces.. Each (?X) says 'next Match/Replace', so your example used 1-replace (with a space) for 30 different matches!
The 1st 'Match/Replace' inserts '0' before 1-digit months, the next two insert 19 or 20 before yy, while also converting MonthNumber ==> :MonthNumber.
The other 12 just convert :MonthNumber ==> MonthAbbreviation
Luuk
 
Posts: 320
Joined: Fri Feb 21, 2020 10:58 pm

Re: How to change date and month into word

Postby lalajee » Fri Jun 25, 2021 10:33 pm

Its not replacing the month with word month.

I try all of the code but still doesnt work. Can you please take screen shot of the setting in Rename bulk to show how you are able to do this.
lalajee
 
Posts: 8
Joined: Wed Jun 23, 2021 11:09 am

Re: How to change date and month into word

Postby Luuk » Sat Jun 26, 2021 12:40 am

Its very good asking for a screen shot, because while creating some test names for the screen shot, Im noticed that not all days do get matched!!
It because Im making some mistakes, but the names getting matched did always convert the months. So before any fixing, must need to know...

1. Can days be 1-digit, just like the months ?
2. Can years be like 99, so needing 19 in front ?
3. Is "words" to be optional? so names like '21 6 25.txt' ===> 2021 Jun 25.txt
Luuk
 
Posts: 320
Joined: Fri Feb 21, 2020 10:58 pm

Re: How to change date and month into word

Postby lalajee » Sat Jun 26, 2021 9:46 am

Luuk wrote:Its very good asking for a screen shot, because while creating some test names for the screen shot, Im noticed that not all days do get matched!!
It because Im making some mistakes, but the names getting matched did always convert the months. So before any fixing, must need to know...

1. Can days be 1-digit, just like the months ?
2. Can years be like 99, so needing 19 in front ?
3. Is "words" to be optional? so names like '21 6 25.txt' ===> 2021 Jun 25.txt


1. Can days be 1-digit, just like the months ? I can't see any files with 1 digit number. All of my days are 2 digit numbers e.g. 03, 10
2. Can years be like 99, so needing 19 in front ? I have files from year 1980 to 2021
3. Is "words" to be optional? so names like '21 6 25.txt' ===> 2021 Jun 25.txt : NO the Month needs to be in word all of the match must be like "Word Word 2021 Jun 25 word word.txt"
lalajee
 
Posts: 8
Joined: Wed Jun 23, 2021 11:09 am

Re: How to change date and month into word

Postby Luuk » Sat Jun 26, 2021 7:04 pm

This will all be easy for the RegEx(1) to conduct, except that Im not understanding about how many total-words?
Do you describe that there should be "at least one-word on each side of the date"?? or maybe do you describe...
"at-least/only 2-words before the date, and at-least/only 2-words after the date"? (because 1st example giving 3 after the date).

Also, Im guessing that "word" does not repeat "same word" four different times, so it could be like "word1 word2 date word3 word4".
Sorry for the confusion in the first answer, because I was copy/pasting from another post, but not really understanding the match.
But its fixed now, so all of the dates do get matched, but I still need to understand about total-words before giving an example.
Luuk
 
Posts: 320
Joined: Fri Feb 21, 2020 10:58 pm

Re: How to change date and month into word

Postby Luuk » Sat Jun 26, 2021 9:23 pm

Also, one question Im forgetting... Should the years like '79' and '22' never be prefixed into '1979' and '2022' ?
The Regex(1) conducts either way, but Im just asking in case the years like this should never be conducted.
Luuk
 
Posts: 320
Joined: Fri Feb 21, 2020 10:58 pm

Re: How to change date and month into word

Postby lalajee » Sun Jun 27, 2021 3:48 pm

Luuk wrote:Also, one question Im forgetting... Should the years like '79' and '22' never be prefixed into '1979' and '2022' ?
The Regex(1) conducts either way, but Im just asking in case the years like this should never be conducted.


Yes please
lalajee
 
Posts: 8
Joined: Wed Jun 23, 2021 11:09 am

Next

Return to Regular Expressions