shenwei356/brename

Specify which matches to replace

Opened this issue · 3 comments

Hi there. I was playing around with this and i can't find a way to select just the desidered matches only. Let me make some examples about this:

  1. Filename01_description_01

I would like to replace just the first "_" with something else, for example " - " so it will be renamed to

Filename01 - description_01

  1. Filename_02_description_02

I would like to replace just the first two "_" with " - " so it will be renamed to:

Filename - 02 - description_02 and so on.

With regex if you want to match just the first "_" you just remove the /g from the expression but this doesn't work in brename

this command:

brename -p "_" -r " - "

replace all the founds "_" into " - "

Hi, brename performs global replacement (with /g switched on).
Since you want to replace the first two "_", it can't be done by just disabling global replacement.
One way is there:

$ brename -p '^([^_]+)_([^_]+)_(.+)' -r '${1} - ${2} - ${3}' -d
Searching for paths to rename...

  [OK] Filename_02_description_02.txt -> Filename - 02 - description_02.txt

1 path(s) to be renamed

Thank you for the answer. What about if it just the first one or if there are many x ones, it will be possible to implement a dedicated switch to make it easier?

just the first one

That would be simpler.

$ brename -p '^([^_]+)_(.+)' -r '${1} - ${2}' -d
Searching for paths to rename...

  [OK] Filename_02_description_02.txt -> Filename - 02_description_02.txt

1 path(s) to be rename

There's no such function to specify which match to replace in regular expressions.