Regex pattern to remove all fullstops(periods) before DATE.
Hello again, sorry to be a bother, but, as title really. I've been trying to create a REGEX to remove all the dots in a string, up until the DATE, ie.
mary.had.a.little.lamb.21.03.2025 I want to read as 'mary had a little lamb - 21.03.2025'
I've been playing on regex101, and this regex works:- [.](?:\d{2}.\d{2}.\d{4})?
Removing all the fullstops, and leaving out the date part to give:-
maryhadalittlelamb
Whereupon I can substitute in a space for the stored matches to give:-
mary had a little lamb
Subsequently adding the date, complete with fullstops back in, but I can't get it to work in Advanced Renamer, it removes ALL the fullstops, not caring about the ' ?: ' non capturing reference.
Am I missing something?
Thanks.
mary.had.a.little.lamb.21.03.2025 I want to read as 'mary had a little lamb - 21.03.2025'
I've been playing on regex101, and this regex works:- [.](?:\d{2}.\d{2}.\d{4})?
Removing all the fullstops, and leaving out the date part to give:-
maryhadalittlelamb
Whereupon I can substitute in a space for the stored matches to give:-
mary had a little lamb
Subsequently adding the date, complete with fullstops back in, but I can't get it to work in Advanced Renamer, it removes ALL the fullstops, not caring about the ' ?: ' non capturing reference.
Am I missing something?
Thanks.
Reply to #1:
Yeah, I've seen that same regex irregularity in Aren regex. I don't sweat it much since I prefer JS over some forms of regex.
Anywho, this should be close to what you want. Create a Script method with this code:
zDateRegEx = new RegExp (/\b\d{2}\.\d{2}\.\d{4}\b/);
sDate = item.newBasename.match (zDateRegEx).pop ();
sDateless = item.newBasename.replace (zDateRegEx, "");
sNewBase = sDateless.replace (/\./g, " ");
//* Debug:
console.log ("sDate: ", sDate);
console.log ("sDateless: ", sDateless);
console.log ("sNewBase: ", sNewBase);
//*/
return sNewBase + "- " + sDate;
Note that using \b and \. reduces the chances of false hits on this regex.
PS: "regex irregularity" is *totally* not a pun. ;)
Yeah, I've seen that same regex irregularity in Aren regex. I don't sweat it much since I prefer JS over some forms of regex.
Anywho, this should be close to what you want. Create a Script method with this code:
zDateRegEx = new RegExp (/\b\d{2}\.\d{2}\.\d{4}\b/);
sDate = item.newBasename.match (zDateRegEx).pop ();
sDateless = item.newBasename.replace (zDateRegEx, "");
sNewBase = sDateless.replace (/\./g, " ");
//* Debug:
console.log ("sDate: ", sDate);
console.log ("sDateless: ", sDateless);
console.log ("sNewBase: ", sNewBase);
//*/
return sNewBase + "- " + sDate;
Note that using \b and \. reduces the chances of false hits on this regex.
PS: "regex irregularity" is *totally* not a pun. ;)
Reply to #1:
Hi,
Try using two 'Replace' methods to achieve your desired result.
First Replace method:
Replace: \.(?=\D)
Replace with: (space)
Regex: yes
This captures all dots following a non-digit character and replaces them with a space. Next, replace the remaining dot before the numeric date.
Second Replace method:
Replace: .
Replace with: -
Occurrence: First
Regex: unchecked
Hope this helps!
Hi,
Try using two 'Replace' methods to achieve your desired result.
First Replace method:
Replace: \.(?=\D)
Replace with: (space)
Regex: yes
This captures all dots following a non-digit character and replaces them with a space. Next, replace the remaining dot before the numeric date.
Second Replace method:
Replace: .
Replace with: -
Occurrence: First
Regex: unchecked
Hope this helps!
Reply to #2: Hi, thankyou for the reply, this does work perfectly!
One day I will have to try and learn JS!
One day I will have to try and learn JS!
Reply to #3:
Hi, I tried your method but while it removes the dots in the example i provided, it doesn't remove them if the string contains other numbers.
Thanks.
Hi, I tried your method but while it removes the dots in the example i provided, it doesn't remove them if the string contains other numbers.
Thanks.
Reply to #5:
I understand the real problem you omitted from your first message.
Try this:
First Replace: this captures a dot before the date and replaces it with a dash between two spaces.
Replace: \.(?=\d{2}\.\d{2}\.\d{4})
Replace with: (space)(dash)(space)
Regex: yes
Second Replace: this captures all dots before a dash and replaces them with a space.
Replace: \.(?=.*-)
Replace with: (space)
Regex: yes
I understand the real problem you omitted from your first message.
Try this:
First Replace: this captures a dot before the date and replaces it with a dash between two spaces.
Replace: \.(?=\d{2}\.\d{2}\.\d{4})
Replace with: (space)(dash)(space)
Regex: yes
Second Replace: this captures all dots before a dash and replaces them with a space.
Replace: \.(?=.*-)
Replace with: (space)
Regex: yes
Reply to #6:
Hi all,
I've been overheating my dog-brain for an hour and can't envision a way to do it in one replace. I did find these (maybe less complex) 2-replace method batches. I put quotes around each one, but of course leave the quotes out:
1)
Replace: "\."
With: " "
(Just replaces all periods with spaces)
Replace: " (\d\d) (\d\d) (\d{4})"
With: " - $1.$2.$3
(Puts in the dash and the periods in the date)
2)
Replace: "(([^\d\.]+)\.)"
With: "$2 "
(This gives you "mary had a little lamb 21.03.2025")
Replace: "(\w) (\d\d\.\d\d\.)"
With: "$1 - $2"
(That should put the dash in the right place unless you have "nn.nn" somewhere else in your filenames)
Another way to do it in script (but it won't work if there's a dash in your filename):
n = item.newBasename.replace( /\.(\d\d\.\d\d\.\d{4})/, " - $1" ) ;
newN = n.split( "-" ) ;
return newN[0].replace( /\./g, " " ) + "-" + newN[1] ;
It just replaces the period before the date with " - "; then it splits the name into two parts at that dash; then it returns the rejoined parts after replacing periods with spaces in the first part. Probably not as universal as Randy's script, but just another way to do it.
Cheers to all! :)
DF
Hi all,
I've been overheating my dog-brain for an hour and can't envision a way to do it in one replace. I did find these (maybe less complex) 2-replace method batches. I put quotes around each one, but of course leave the quotes out:
1)
Replace: "\."
With: " "
(Just replaces all periods with spaces)
Replace: " (\d\d) (\d\d) (\d{4})"
With: " - $1.$2.$3
(Puts in the dash and the periods in the date)
2)
Replace: "(([^\d\.]+)\.)"
With: "$2 "
(This gives you "mary had a little lamb 21.03.2025")
Replace: "(\w) (\d\d\.\d\d\.)"
With: "$1 - $2"
(That should put the dash in the right place unless you have "nn.nn" somewhere else in your filenames)
Another way to do it in script (but it won't work if there's a dash in your filename):
n = item.newBasename.replace( /\.(\d\d\.\d\d\.\d{4})/, " - $1" ) ;
newN = n.split( "-" ) ;
return newN[0].replace( /\./g, " " ) + "-" + newN[1] ;
It just replaces the period before the date with " - "; then it splits the name into two parts at that dash; then it returns the rejoined parts after replacing periods with spaces in the first part. Probably not as universal as Randy's script, but just another way to do it.
Cheers to all! :)
DF