Merge branch 'master' into dependabot/pip/pylint-3.1.0

This commit is contained in:
Jesse Bannon 2024-04-28 09:09:08 -07:00
commit 562fbd946d
3 changed files with 35 additions and 0 deletions

View file

@ -560,6 +560,15 @@ regex_search
the string as the first element of the Array. If there are capture groups, returns each the string as the first element of the Array. If there are capture groups, returns each
group as a subsequent element in the Array. group as a subsequent element in the Array.
regex_sub
~~~~~~~~~
:spec: ``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.
---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------
String Functions String Functions

View file

@ -52,3 +52,13 @@ class RegexFunctions:
Returns number of capture groups in regex Returns number of capture groups in regex
""" """
return Integer(re.compile(regex.value).groups) 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))

View file

@ -43,3 +43,19 @@ class TestNumericFunctions:
def test_regex_fullmatch(self, values: str, expected_output: str): def test_regex_fullmatch(self, values: str, expected_output: str):
output = single_variable_output(f"{{%regex_fullmatch({values})}}") output = single_variable_output(f"{{%regex_fullmatch({values})}}")
assert output == expected_output assert output == expected_output
@pytest.mark.parametrize(
"values, expected_output",
[
("'[^A-Za-z0-9 ]', '', 'This title is AWESOME!!'", "This title is AWESOME"),
("'\s+', '_', 'Consolidate spaces'", "Consolidate_spaces"),
(
"'(words) are (reordered)', '\\2 are \\1', 'Oh words are reordered'",
"Oh reordered are words",
),
("'MATCH', '', 'matcha is great'", "matcha is great"),
],
)
def test_regex_sub(self, values: str, expected_output: str):
output = single_variable_output(f"{{%regex_sub({values})}}")
assert output == expected_output