0

I am completely new to regex and I would greatly appreciate any help.

I am trying to bulk rename files.

File names are:

001 This is the first file.txt
002 This is the second file.txt
003 This is the third file.txt

And I would like to change them to:

001 - This is the first file.txt
002 - This is the second file.txt
003 - This is the third file.txt

What should be the regex syntax I should be using?

I will be using Multiple File Rename tool in Google Drive and it is using RegExp Tester site (http://iblogbox.com/devtools/regexp) to test the syntax and have a replacement text as a separate input.

screenshot of the input parameters

2
  • 1
    Add an OS tag to the question.
    – somebadhat
    Apr 23, 2020 at 3:47
  • What language/tool are you using?
    – Toto
    Apr 23, 2020 at 10:59

2 Answers 2

0

As you're using Windows, I propose a solution with Notepad++:

  • Ctrl+H
  • Find what: ^\S+\h\K
  • Replace with: - (an hyphen and a space)
  • CHECK Wrap around
  • CHECK Regular expression
  • Replace all

Explanation:

^           # beginning of line
  \S+       # 1 or more non spaces
  \h        # 1 horizontal space
  \K        # forget all we have seen until this position

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

1
  • @Tolo: I have moved the files to Gdrive and using an app called "Multiple File Rename" and it uses Regex tester at: iblogbox.com/devtools/regexp and will be using the syntax there. Sorry for the trouble. This is good for future reference when I have to deal with files on Windows OS. Thanks for your detailed steps to arrive at a solution.
    – BluFyan
    Apr 24, 2020 at 0:32
0

If you're using sed, for example, you could use:

's@[0-9]\{3\} @&- @'

This will match the first occurrence of 3 digits followed by a space, and replace that occurrence with itself (leave it as it was) via the backreference, with the '- ' appended. Without a 'g' option at the end, this will only match 1 occurrence.

I've used '@'s as delimiters here for readability instead of the standard forward slash. The effect is the same.

For RegExr.com, you would select the Text button, the Replace tool, and the PCRE RegEx Engine instead of JavaScript, then put:

"([0-9]{3} )"

(without the quotes) into the Expression field, and:

"\1- "

(without the quotes, but with the trailing space) into the Tools field. Put your text to be modified into the Text field and make sure global is the only flag selected in Flags.

If your regex replacement tool doesn't support backreferences or preserving matched text (like the \K from Toto's answer) for some reason, you can use the following matching expression:

"Th"

(without quotes) and this replacement string:

"- Th"

(without quotes) provided that all of your files exactly follow the pattern you described.

9
  • I am using RegExp Tester site to test the syntax and have a replacement text as a separate input. I tried the first part of your syntax and did not work. For example, \s finds all the blank spaces but how to pick the first occuring space only is my question? Sorry vert new to this hence asking for explanation.
    – BluFyan
    Apr 23, 2020 at 5:53
  • \s - finds all the spaces Still trying to figure out how to nail down the first occurrence
    – BluFyan
    Apr 23, 2020 at 6:05
  • @BluFyan Did you mean RegExr.com? I've updated my answer with information for RegExr.com, which will do the job, so I suggest you use that.
    – malik11
    Apr 23, 2020 at 6:43
  • I am testing the syntax at iblogbox.com/devtools/regexp "([0-9]{3} )" gives back " - This is the first file.txt" for the text "123 This is the first file.txt" when using " - " as the replacement text. "\1- " as a replacement text (syntax) does not work.
    – BluFyan
    Apr 24, 2020 at 0:28
  • @BluFyan I've updated this answer with a new solution. That replacement string didn't work because this tool appears to support neither backreferences nor match preservation, which substantially limits such a tool.
    – malik11
    Apr 24, 2020 at 3:12

You must log in to answer this question.

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