Change match groups format from \1, \2, etc

To: {1},  {2}, etc
This commit is contained in:
Pedro de Oliveira 2023-05-10 20:15:47 +01:00
parent 83464880a8
commit 1d306c8c6c
3 changed files with 9 additions and 9 deletions

View File

@ -30,7 +30,7 @@ You can use matching groups in the pattern to capture parts of the file name tha
The `--replacement` argument specifies the replacement pattern for the matched file names. This argument is required.
You can use reference groups in the pattern by using `\1`, `\2`, etc. to refer to the captured groups in the `--pattern` argument. For example, if you have a pattern `"S(\d+)E(\d+)"` that matches `"S01E11"` and you want to replace it with `"Season 01 Episode 11"`, you can use the following replacement pattern: `"Season \1 Episode \2"`.
You can use reference groups in the pattern by using `{1}`, `{2}`, etc. to refer to the captured groups in the `--pattern` argument. For example, if you have a pattern `"S(\d+)E(\d+)"` that matches `"S01E11"` and you want to replace it with `"Season 01 Episode 11"`, you can use the following replacement pattern: `"Season {1} Episode {2}"`.
You can also use the following special tags in the replacement pattern:
@ -47,10 +47,10 @@ For example, if you want to only rename files with the `.mkv` extension, you can
### Example
```shell
$ rmv --path /path/to/files --pattern "S(\d+)E(\d+)" --replacement "Season \1 Episode \2.{ext}" --wildcard "*.mkv"
$ rmv --path /path/to/files --pattern "S(\d+)E(\d+)" --replacement "Season {1} Episode {2}.{ext}" --wildcard "*.mkv"
```
This will match all file names in the directory that match the pattern `"S(\d+)E(\d+)"` and replace them with the pattern `"Season \1 Episode \2.{ext}"`. Only files with the `".mkv"` extension will be considered.
This will match all file names in the directory that match the pattern `"S(\d+)E(\d+)"` and replace them with the pattern `"Season {1} Episode {2}.{ext}"`. Only files with the `".mkv"` extension will be considered.
```shell
mv "Not Warez - S01E16 - HDTV-720p.mkv" "Season 01 Episode 16.mkv"
@ -76,12 +76,12 @@ Here are the available options:
-r, --replacement <REPLACEMENT>
Replacement pattern for the matched file names.
You can use `\1`, `\2`, etc. to reference the match groups in the pattern.
You can use `{1}`, `{2}`, etc. to reference the match groups in the pattern.
There are also the following tags available:
* {full_name} - file name with extension
* {name} - file name without extension
* {ext} - extension
Example: "Season \1 Episode \2.{ext}"
Example: "Season {1} Episode {2}.{ext}"
-w, --wildcard <WILDCARD>
File name wildcard to filter the files to be renamed in the directory.

View File

@ -64,7 +64,7 @@ pub fn replace_name(file_name: &str, pattern: &str, replacement: &str) -> String
for (i, capture) in captures.iter().enumerate() {
if let Some(capture) = capture {
let capture_text = capture.as_str().to_owned();
let replace_key = format!("\\{i}");
let replace_key = format!("{{{i}}}");
replaced_text = replaced_text.replace(&replace_key, &capture_text);
}
}
@ -79,7 +79,7 @@ fn groups_and_ext_test() {
const RESULT: &str = "Season 1 - Episode 16 - The Best Show - Bla Bla Bla - 720p-HDTV.mkv";
const PATTERN: &str = r"(.*) - S0?(\d+)E0?(\d+) - (.*) (HDTV|WEBDL)-(720p|1080p)";
const REPLACEMENT: &str = r"Season \2 - Episode \3 - \1 - \4 - \6-\5.{ext}";
const REPLACEMENT: &str = r"Season {2} - Episode {3} - {1} - {4} - {6}-{5}.{ext}";
assert_eq!(replace_name(INPUT, PATTERN, REPLACEMENT), RESULT);
}

View File

@ -27,7 +27,7 @@ struct Args {
/// Replacement pattern for the matched file names.
///
/// You can use `\1`, `\2`, etc. to reference the match groups in the pattern.
/// You can use `{1}`, `{2}`, etc. to reference the match groups in the pattern.
///
/// There are also the following tags available:
///
@ -37,7 +37,7 @@ struct Args {
///
/// * {ext} - extension
///
/// Example: "Season \1 Episode \2.{ext}"
/// Example: "Season {1} Episode {2}.{ext}"
#[arg(short = 'r', long)]
replacement: String,