How cut away a part of a text string included

Bulk Rename Utility How-To's

How cut away a part of a text string included

Postby torredipisa » Thu Jul 28, 2022 11:40 am

Good Morning

I have a list of files each containing (in variable positions) a fixed length string like: 20150103
My aim is to mantain the first four chars (2015) and cut the remaining text (0103) away.
Thanks for your help.

Fabio
torredipisa
 
Posts: 9
Joined: Thu Jul 28, 2022 11:28 am

Remove mmdd from yymmdd

Postby Luuk » Thu Jul 28, 2022 4:05 pm

Greetings Fabio.. With Regex(1) having a checkmark inside for "v2", the "Match" and "Replace" can be like...
(?<!\d)(19|20)(\d\d)\d{4}(?!\d)/g
$1$2

aa-20150103-zz ==============> aa-2015-zz
abcd-19980714-zz ============> abcd-1998-zz
aa20150103-20220103zz =======> aa2015-2022zz
Begin 9201501039 End ========> (no changes, because date has numbers touching)
Begin--18150103--End ========> (no changes, because date not starts with 19-or-20)

If only wanting to conduct the very first date inside of filenames, can remove /g at very end of the "Match".
Luuk
 
Posts: 689
Joined: Fri Feb 21, 2020 10:58 pm

Re: How cut away a part of a text string included

Postby torredipisa » Thu Jul 28, 2022 5:13 pm

Great! It Works!
Thank You very much for your help.
FAbio
torredipisa
 
Posts: 9
Joined: Thu Jul 28, 2022 11:28 am

Re: How cut away a part of a text string included

Postby torredipisa » Thu Jul 28, 2022 6:57 pm

Could you kindly explain the meaning of the script code:
for instance 19|20 means that the string valued can begin with 19 or 20 i suppose
but the other codings i cannot understand.
Thank you
Fabio
torredipisa
 
Posts: 9
Joined: Thu Jul 28, 2022 11:28 am

Remove mmdd from yyyymmdd

Postby Luuk » Thu Jul 28, 2022 10:07 pm

(Not digit) (19--or--20) (2-digits) 4-digits (Not digit)/all-matches
(?<!\d)(19|20)(\d\d)\d{4}(?!\d)/g
$1$2

(?<!\d) ----> To the left cannot be a digit
(19|20) ---> 19-or-20
(\d\d) -----> any 2-digits: 0-9
\d{4} ------> any 4-digits: 0-9 (same as \d\d\d\d)
(?!\d) -----> To the right cannot be a digit
/g ---------> Says global for all-matches, instead of 1st-match

The red (groups) is called look-arounds and they do only match conditions, without capturing any of the characters.
So this is why the $1 gets married to (19|20) for 'Group1', and the $2 gets married to (\d\d) for 'Group2'.

The \d{4} is not (grouped) because we dont need $3 to put the last 4-digits back into the filename.
You could always group it like (\d{4}) just to make the "Match" part look better for the eyesight.
But it still conducts exactly the same, unless adding something like --$3 into your "Replace".
Luuk
 
Posts: 689
Joined: Fri Feb 21, 2020 10:58 pm

Re: How cut away a part of a text string included

Postby torredipisa » Fri Jul 29, 2022 9:06 am

Excellent explanation!
Very obliged.
Thank You so much
Fabio
torredipisa
 
Posts: 9
Joined: Thu Jul 28, 2022 11:28 am


Return to How-To