regex remove pattern then copy to beginning of filename

Advanced Renamer forum
#1 : 15/07-16 16:31
Bruno Marchesi
Bruno Marchesi
Posts: 1

Given these examples:

Company 1 - cycle 2005-2006 - resource allocation.doc
Research company - years 2005-2006 - priorities.doc
Maintenance schedules - company x paper 2008.doc

One first approach, desired output would be:

2005.Company 1 - cycle -2006 - resource allocation.doc
2005.Research company - years -2006 - priorities.doc
2008.Maintenance schedules - company x paper .doc

Other words, how could one extract a regex pattern, like (\d\d\d\d) and associate
it output (like \1) to the beginning of a filename?

Thanks in advance,

15/07-16 16:31
#2 : 16/07-16 11:33
Posts: 92
Reply to #1:
Try this Replace Method.

Text to be replaced: (.*?)(\d{4})(.*)
Replace with: \2.\1\3
Occurrence: All
Case sensitive: Doesn't matter
Use regular expression: Ticked
Apply to: Name

The idea is to capture the characters before the 1st 4-digit sequence (as group 1), the 4-digits themselves as group 2, and then the remainder as group 3. The groups are defined by the parentheses '()'. The groups can then be referenced (or 'associated') by its ordinal position, using the character sequence \#, where # is the group ordinal position. For example, \1 refers to the first group.

Note that the first group is (.*?) as opposed to just (.*). The difference is that the former variation will take the first 4-digits found, while the latter will take the last digits found. The ? is a qualifier which tells the regex to not be greedy, so it stops at the first instance. Without the ? qualifier, the regex will continue to look for all subsequent digit sequences too and only stop at the last one.

In your final example, there is a trailing space, but you can just add a separate method to trim spaces.

16/07-16 11:33