Skip to content

Commit 3eb9ce8

Browse files
feat: Add regex_replace Jinja filter for low-code connector builder (#904)
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 796bb34 commit 3eb9ce8

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

airbyte_cdk/sources/declarative/interpolation/filters.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ def regex_search(value: str, regex: str) -> str:
136136
return ""
137137

138138

139+
def regex_replace(value: str, regex: str, replacement: str) -> str:
140+
"""
141+
Replace all occurrences of a regular expression pattern in a string.
142+
"""
143+
return re.sub(regex, replacement, value)
144+
145+
139146
def hmac(value: Any, key: str, hash_type: str = "sha256") -> str:
140147
"""
141148
Implementation of a custom Jinja2 hmac filter with SHA-256 support.
@@ -181,6 +188,7 @@ def hmac(value: Any, key: str, hash_type: str = "sha256") -> str:
181188
base64binascii_decode,
182189
string,
183190
regex_search,
191+
regex_replace,
184192
hmac,
185193
]
186194
filters = {f.__name__: f for f in _filters_list}

unit_tests/sources/declarative/interpolation/test_filters.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,36 @@ def test_regex_search_no_match() -> None:
108108
assert val is None
109109

110110

111+
@pytest.mark.parametrize(
112+
"expression, expected",
113+
[
114+
pytest.param(
115+
"{{ 'hello world' | regex_replace('world', 'there') }}",
116+
"hello there",
117+
id="basic_replacement",
118+
),
119+
pytest.param(
120+
"{{ 'abc123def456' | regex_replace('[0-9]+', '') }}",
121+
"abcdef",
122+
id="regex_pattern_strip_digits",
123+
),
124+
pytest.param(
125+
"{{ 'hello world' | regex_replace('xyz', 'replaced') }}",
126+
"hello world",
127+
id="no_match_returns_original",
128+
),
129+
pytest.param(
130+
"{{ 'aaa bbb aaa' | regex_replace('aaa', 'ccc') }}",
131+
"ccc bbb ccc",
132+
id="multiple_occurrences",
133+
),
134+
],
135+
)
136+
def test_regex_replace(expression: str, expected: str) -> None:
137+
val = interpolation.eval(expression, {})
138+
assert val == expected
139+
140+
111141
def test_hmac_sha256_default() -> None:
112142
message = "test_message"
113143
secret_key = "test_secret_key"

0 commit comments

Comments
 (0)