feat: add matches_regex filter and string_matching_regex expression test - #1332
Open
craigayre wants to merge 1 commit into
Open
feat: add matches_regex filter and string_matching_regex expression test#1332craigayre wants to merge 1 commit into
craigayre wants to merge 1 commit into
Conversation
There's no direct way to test whether a string matches a regex. We've
been working around it with things like:
{% if var and var|regex_replace('^pattern$', '') == '' %}
This adds a matches_regex filter and a string_matching_regex expression
test so you can write:
{{ 'hello'|matches_regex('^[a-z]+$') }}
{% if var is string_matching_regex '[0-9]+' %}
{{ items|selectattr('name', 'string_matching_regex', '^foo') }}
Both use RE2 (com.google.re2j) consistent with the existing
regex_replace filter. Both use partial matching (Matcher.find), anchor
with ^...$ for full-string matching.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
There's no direct way to test whether a string matches a regex. We've been working around it with things like:
This adds a
matches_regexfilter and astring_matching_regexexpression test so you can write:Both use RE2 (
com.google.re2j) consistent with the existingregex_replacefilter. Both use partial matching (Matcher.find), anchor with^...$for full-string matching.Testing
MatchesRegexFilterTest(9 tests): matching/non-matching, anchored patterns, null input, null arg, missing arg, invalid regex,SafeString, andmaxStringLengthenforcement.IsStringMatchingRegexExpTestTest(5 tests): matching/non-matching, null arg,SafeString, and end-to-endselectattrfiltering.Notes
matches_regex/string_matching_regex) if there's something more appropriate.string_containingexpression test andregex_replacefilter. There could probably be some simplifications, but kept things consistent with the existing implementations intentionally.🤖 Claude Code assisted.