Deleting year and all characters to the right

A swapping-ground for Regular Expression syntax

Deleting year and all characters to the right

Postby paoloandnikki » Mon Sep 07, 2020 8:52 pm

I would like to know how to delete a 4-digit year, 1 space before said year and all characters to the right of it. I can't seem to figure out the proper RegEx expression for it. Thank you!

Input: "Document 1 2005 Dec 20.doc"
to
Output:"Document 1.doc"

Thank you very much!
paoloandnikki
 
Posts: 3
Joined: Mon Sep 07, 2020 8:43 pm

Re: Deleting year and all characters to the right

Postby Admin » Tue Sep 08, 2020 6:15 am

Hi, is it always to delete everything after and including the second space? Or could it be the first space?
Admin
Site Admin
 
Posts: 1896
Joined: Tue Mar 08, 2005 8:39 pm

Re: Deleting year and all characters to the right

Postby paoloandnikki » Tue Sep 08, 2020 2:36 pm

Hi! Yes. I need to delete the space before the four digit year, the four digit year itself and everything after that. I have 500 of these files and I could really use the help.

Example:
Document ABCD 2020 - 12-23 ver 5.doc
Document ABCD.doc

Document - 12 2001-2002.doc
Document - 12.doc

Thank you!
paoloandnikki
 
Posts: 3
Joined: Mon Sep 07, 2020 8:43 pm

Re: Deleting year and all characters to the right

Postby therube » Tue Sep 08, 2020 7:46 pm

I think this will do it. Test & see if it looks right.

1:RegEx
Code: Select all
Match:  (.*?)(\s\d\d\d\d[^\d*])
Replace:  \1


Match anything
Up to (the first) sequence of <space>4-digits, followed by anything that is not another digit (so it does not match 5 or more digits).

Only keep up to the <space>4-digits

Note this does not find names that end with <space>4-digits, 12 2001.doc.
^-Hint.
therube
 
Posts: 921
Joined: Mon Jan 18, 2016 6:23 pm

Re: Deleting year and all characters to the right

Postby Admin » Wed Sep 09, 2020 1:00 am

Match
(.*)\s(\d\d\d\d)
Replace
\1

Will drop all text after the last space + 4 digits , including the space and 4 digits,
Admin
Site Admin
 
Posts: 1896
Joined: Tue Mar 08, 2005 8:39 pm

Re: Deleting year and all characters to the right

Postby Admin » Wed Sep 09, 2020 8:14 am

Or

Match
(.*)\s\d\d\d\d
Replace
\1

or

Match
(.*) \d\d\d\d
Replace
\1

--------------------------------------------------------------------------------------------------------------------------------------------
Note:
When enabling v2 regular expressions in BRU 3.4.0.0 or newer the unmatched text is copied to the result, not dropped like v1, so

Match
(.*) \d\d\d\d
Replace
\1

Would just remove the year.

Match
(.*) \d\d\d\d(.*)
Replace
\1

Would drop year and text after.
Admin
Site Admin
 
Posts: 1896
Joined: Tue Mar 08, 2005 8:39 pm

Re: Deleting year and all characters to the right

Postby therube » Wed Sep 09, 2020 4:55 pm

Match
(.*)\s(\d\d\d\d)
Replace
\1

Will drop all text after the last space + 4 digits , including the space and 4 digits.

Isn't that overly broad, cause that will also find 5 (or more digits - which then isn't a "year").

12 20011-2002.doc
-> 12.doc

rather then being left unchanged.

Now that does work for:

12 20011 2002
-> 12 20011

which mine does not, though you might want an anchor in there with that, (.*)\s(\d\d\d\d)$
therube
 
Posts: 921
Joined: Mon Jan 18, 2016 6:23 pm

Re: Deleting year and all characters to the right

Postby Admin » Wed Sep 09, 2020 8:26 pm

Yes absolutely, your regex is more strict and matches just 4 digits but not 5 or more digits. One could also want to make sure the first digit is a 2 or 1 if matching a year. It is also a choice between a more complex regex or a simpler regex that is good enough for the specific set of file names being processed.
Admin
Site Admin
 
Posts: 1896
Joined: Tue Mar 08, 2005 8:39 pm

Re: Deleting year and all characters to the right

Postby paoloandnikki » Mon Sep 14, 2020 8:20 pm

I just wanted to say thank you for helping me out. I sincerely appreciate it. Bulk Rename Utility is an awesome piece of software. :D
paoloandnikki
 
Posts: 3
Joined: Mon Sep 07, 2020 8:43 pm


Return to Regular Expressions