From 20a94d9a4f527fe29cca52038e5410993cb7f641 Mon Sep 17 00:00:00 2001 From: Tomas Babej Date: Thu, 25 Apr 2024 21:05:09 -0400 Subject: [PATCH] regex_functions: Add regex_sub This implements %regex_sub built-in function to enhance string-processing capabilities, allowing users to perform substitutes like: * removing non-ascii characters * replacing subsequent whitespace characters with a single whitespace --- src/ytdl_sub/script/functions/regex_functions.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/ytdl_sub/script/functions/regex_functions.py b/src/ytdl_sub/script/functions/regex_functions.py index f837be32..feb204ce 100644 --- a/src/ytdl_sub/script/functions/regex_functions.py +++ b/src/ytdl_sub/script/functions/regex_functions.py @@ -52,3 +52,13 @@ class RegexFunctions: Returns number of capture groups in regex """ return Integer(re.compile(regex.value).groups) + + @staticmethod + def regex_sub(regex: String, replacement: String, string: String) -> String: + """ + :description: + Returns the string obtained by replacing the leftmost non-overlapping occurrences of the + pattern in string by the replacement string. The replacement string can reference the + match groups via backslash escapes. Callables as replacement argument are not supported. + """ + return String(re.sub(regex.value, replacement.value, string.value))