#1 2010-05-19 08:30

yesterdayer
Member
Registered: 2010-05-19
Posts: 4

Surround with brackets 4 digit numbers

Hello.

I've really been enjoying using ReNamer & have found it to be a great tool.

But I can't seem to figure out yet how to do this one...
I have a large number of files that have year numbers within the file names and need to put those years into brackets.
Rounded brackets (parentheses) that is.

Like so:

Before
beach front #01 1997 retouched.txt
beach front #02 1997 retouched.txt
front lawn #01 1999 untouched.txt
front lawn #02 1999 retouched.txt
back garden #01 2007 unchecked.txt
back garden #02 2008 retouched.txt

After
beach front #01 (1997) retouched.txt
beach front #02 (1997) retouched.txt
front lawn #01 (1999) untouched.txt
front lawn #02 (1999) retouched.txt
back garden #01 (2007) unchecked.txt
back garden #02 (2008) retouched.txt

I'm sure it's a simple regular expression rule, but I just can't figure it out on my own...

Thanks in advance.

Offline

#2 2010-05-19 11:02

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,379

Re: Surround with brackets 4 digit numbers

Expression: \b(\d{4})\b
Replace: ($1)

This will take all 4 digit numbers and surround them with brackets.

Offline

#3 2010-05-19 11:37

yesterdayer
Member
Registered: 2010-05-19
Posts: 4

Re: Surround with brackets 4 digit numbers

Thank you very much. That works well. smile


But I was just checking & there are some files mixed in the same folder that also have the month added.
They look like 1998-01.

Is there an easy way to make an expression that will work for both?

I tried this for the ones with months based on what you showed me and it worked on the couple that I moved out of the folder to test:

Expression \b(\d{4}-\d{2})\b
Replace ($1)

But using them as two separate rules in the same folder ends up looking like this:
((1998)-01)

There are too many of the ones with months included to find, move out to a separate folder, rename then move them back again...

Offline

#4 2010-05-19 12:03

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,379

Re: Surround with brackets 4 digit numbers

Expression: \b([-\d]{4,})\b

This will find a any combination of digits and dash with length of 4 characters of more. This is a very flexible version, since it does not define the order at which digits and dash should appear.

Expression: \b((\d{4}\-\d{2})|(\d{4}))\b

This one is more strict. It will match either of the 2 patterns: NNNN-NN or NNNN - where N is a digit.

Offline

#5 2010-05-19 19:53

yesterdayer
Member
Registered: 2010-05-19
Posts: 4

Re: Surround with brackets 4 digit numbers

Perfect.

The 2nd more strict one is ideal & the 1st one helped me understand more about regular expressions. cool

Thanks much for your help & quick responses.

And thanks for ReNamer.
I like it. It is better than other similar apps that I've used. Smaller footprint on my system and easy to use yet powerful.
I recommend it to my colleagues and they also seem to like it as well.

Cheers!

Offline

#6 2010-05-19 21:14

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: Surround with brackets 4 digit numbers

Good work Denis.
May i add an little bit cleaner regex?: (\d\d\d\d)(-\d\d)?


FROM:
beach front #01 1997 retouched.txt
beach front #02 1997-01 retouched.txt
front lawn #01 1999 untouched.txt
front lawn #02 1999-03 retouched.txt
back garden #01 2007 unchecked.txt
TO:
beach front #01 (1997) retouched.txt
beach front #02 (1997-01) retouched.txt
front lawn #01 (1999) untouched.txt
front lawn #02 (1999-03) retouched.txt
back garden #01 (2007) unchecked.txt

DO:
Add Rule RegEx.
Expression: (\d\d\d\d)(-\d\d)?
Replace: ($1$2)

Explanation:
(\d\d\d\d) >>> means:  match four digits and group them by (...) to use the match as backreference by $1.
(-\d\d)? >>> means due the "?":  match one-or-none "dash followed by two digits" and reuse an match, if any,  by $2. If there is no match, $2 simple give us nothing.

And of course as Denis shows, to be sure to match the right digits,
surround the regex by  "\b" which means match an word boundary on each side:
Expression: \b(\d\d\d\d)(-\d\d)?\b
Replace: ($1$2)

ReNamer_RegEx001_c16.png


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#7 2010-05-20 13:39

yesterdayer
Member
Registered: 2010-05-19
Posts: 4

Re: Surround with brackets 4 digit numbers

That's a good one too Stefan.

And thanks for the explanation as well. smile
I have always found that learning by way of example to be a valuable thing.

Last edited by yesterdayer (2010-05-20 13:43)

Offline

Board footer

Powered by FluxBB