Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using a PCRE compatible file rename program. Is there a character or pattern I can type in the "replace" field of the program to tell it to append numbers at the end of file names and that they should be increasing for each new file?

Here is an example I made on the fly.

doc1 - Kopia (2) - Kopia.txt
doc1 - Kopia (2) - Kopia_2.txt
doc1 - Kopia (2).txt
doc1 - Kopia (2)_2.txt
doc1 - Kopia (3).txt
doc1 - Kopia (3)_2.txt
doc1 - Kopia - Kopia (2).txt
doc1 - Kopia - Kopia (2)_2.txt
doc1 - Kopia - Kopia - Kopia.txt
doc1 - Kopia - Kopia - Kopia_2.txt
doc1 - Kopia - Kopia.txt
doc1 - Kopia - Kopia_2.txt
doc1 - Kopia.txt
doc1 - Kopia_2.txt

I want to retain "doc" at the beginning and I want to retain the file name extension. The program has a check mark that either enables or disables processing of extensions. So if I leave that unchecked I should not have to worry about that. I want the number after "doc" to increase for each file, since I am not allowed to have several files with the same same name in the same folder.

So this is what it should end up with.

etc... you get the idea

I only use .* as the "match" pattern. And I figure the "replace" pattern should be something like doc{n} but what should I type in place of {n} I have no idea. Is there any simple pattern like that, that I can use here?

share|improve this question
up vote 1 down vote accepted

No, there is no regular expression construct for this. Regular expressions do one thing: they match patterns and allow you to refer to captured patterns. What you are asking for would require incrementing a variable and so would enter the realm of programming languages. Such a feature is beyond the scope of regular expressions.

If your program supports evaluating arbitrary code as part of its matching, something like s/foo/1+2/ to substitute foo with 3 you might be able to do this but it is not a feature of regexes.

share|improve this answer
I feared so... but I have found another way to do it. The program has a numbering option for this purpose. What would the regex pattern be to match only file names with upper case extensions like JPEG or a mix of upper and lower like Jpeg? – sammyg Jun 23 '13 at 13:16
@Sammy in PCRE, to match all upper case: /\.[A-Z]+$/, to match all lower case: /\.[a-z]+$/, to match both upper and lower case: /\.[a-zA-Z]+$/ and to match extensions that start with upper case and continue in lower case: /\.[A-Z][a-z]+$/. – terdon Jun 23 '13 at 13:33
Thank you very much! – sammyg Jun 23 '13 at 13:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .