From 1d306c8c6cdc0a9a2ce88b2bf8eb0d3d329566f4 Mon Sep 17 00:00:00 2001 From: Pedro de Oliveira Date: Wed, 10 May 2023 20:15:47 +0100 Subject: [PATCH] Change match groups format from \1, \2, etc To: {1}, {2}, etc --- README.md | 10 +++++----- src/lib.rs | 4 ++-- src/main.rs | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 14730d5..b37fe8c 100644 --- a/README.md +++ b/README.md @@ -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 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 File name wildcard to filter the files to be renamed in the directory. diff --git a/src/lib.rs b/src/lib.rs index 7398eb3..3d268d1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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); } diff --git a/src/main.rs b/src/main.rs index 14d3bee..70776d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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,