• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Replace last (4) characters in a filename

Community Beginner ,
Nov 08, 2017 Nov 08, 2017

Copy link to clipboard

Copied

Using the batch rename feature in Bridge, is there a way to replace a specific amount of characters before an extension (.png). I know there's a way to replace first 4 characters with ^.{4} But, I'm not able to figure out how to do this towards the end of file. Is this possible?

Views

7.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 09, 2017 Nov 09, 2017

A little bit late to the party…

I know there's a way to replace first 4 characters with ^.{4} But, I'm not able to figure out how to do this towards the end of file. Is this possible?

What you are looking for is the $ dollar symbol, which will base the regex from the end of the string, rather than the start of the string which is what the ^ caret symbol is for… However, it is of course not that easy!

Simply using .{4}$ will chop off the . period and three filename extensions, so we can’t use that:

f

...

Votes

Translate

Translate
Guide ,
Nov 09, 2017 Nov 09, 2017

Copy link to clipboard

Copied

2017-11-09_135758.jpg

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 09, 2017 Nov 09, 2017

Copy link to clipboard

Copied

A little bit late to the party…

I know there's a way to replace first 4 characters with ^.{4} But, I'm not able to figure out how to do this towards the end of file. Is this possible?

What you are looking for is the $ dollar symbol, which will base the regex from the end of the string, rather than the start of the string which is what the ^ caret symbol is for… However, it is of course not that easy!

Simply using .{4}$ will chop off the . period and three filename extensions, so we can’t use that:

filename1234.jpg = filename1234

anothername-abc.tiff = anothername-abc.

Notice that on a 4 character extension, the dot is left, so we would need a more flexible approach, such as \..{1,} – however that does not solve the fundamental issue with the regex. So a different approach is required. The great thing about regular expressions is that there are many valid alternatives that can achieve the same result. Some are concise or verbose, some are more robust than others and may or may not “fail” with unexpected minor variations to the pattern.

Another viable regex alternative to the method outlined by SuperMerlin is:

.{4}(\..{1,})$

regex.png

This valid but “less than ideal” regex works in Adobe Bridge as it has special handling of filename extensions, even though the regex is removing the last 4 characters and the filename extension, Bridge is smart enough to retain the filename extension even though it was explicitly removed by the regex. This would not work the same way in a text editor for example, as the extension would be removed.

A more robust and technically better approach would gracefully handle the filename extension (even though Bridge has a safety net with extensions)…

Find: (.+)(.{4})(\..{1,})

Replace: $1$3

The $ at the end of the regex is now superfluous in this new improved regex – as the capture groups are specifying explicit positions/patterns:

regex2.png

Which is a slightly different way of expressing the same regex as originally proposed by SuperMerlin, so I have come full circle with this second “refined” regex example! :]

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 09, 2017 Nov 09, 2017

Copy link to clipboard

Copied

Perfect! Thank you so much for this detailed description. These scripts make more sense and are more manageable.

My favorite is .{4}(\..{1,})$

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 09, 2017 Nov 09, 2017

Copy link to clipboard

Copied

Not a problem, other valid variations in Bridge also include:

.{4}\..+$

.{4}(\..+)$

.{4}(?:\..+)$

You may find the following regex testers helpful in working out what is going on:

www.regexr.com

www.regex101.com

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 10, 2017 Nov 10, 2017

Copy link to clipboard

Copied

LATEST

OK, I figured out how to use the simple regex, ignoring the filename extension by using a combo of filename/name (which trims off the extension) which is then “piped” to an intermediate filename for use with the simplified regex:

.{4}$

regular_expression.png

Which is just a “GUI” based method of dealing with the file extension, which is of course also possible using a couple of regex’s chained together:

stringsubs.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines