Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify which matches to replace #35

Open
graphixillusion opened this issue Apr 5, 2024 · 3 comments
Open

Specify which matches to replace #35

graphixillusion opened this issue Apr 5, 2024 · 3 comments

Comments

@graphixillusion
Copy link

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 " - "

@shenwei356
Copy link
Owner

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

@graphixillusion
Copy link
Author

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?

@shenwei356
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants