Add regular expression matching #524
Open
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.
This patchset implements regular expression matching for abs.
A new type REGEX is implemented, and a set of functions, that will perform the regular expression matching. These functions can be called with STRING or REGEX objects as their first (pattern) argument. In case of a STRING argument, the string will be compiled to a REGEX on every call, otherwise the REGEX object will be used immediately.
As go's regexp package is used, the regular expression syntax is that of RE2, as described here: https://github.com/google/re2/wiki/Syntax except for \C
Tests have been added to evaluator/builtin_functions_test.go for all new functions.
Functions:
regex(string)
compile string to a regular expression
matches(pattern, string)
returns boolean true if the string matches the pattern, false if it does not match. pattern can be either a regex or a string.
match(pattern, string)
match the pattern against a string. pattern can be either a regex or a string. If the pattern does not match the string, returns null, otherwise returns an array where the first item is the full match, and all items after the first are the corresponding submatches.
match_all(pattern, string)
match the pattern against string. pattern can be either a regex or a string. If the pattern does not match the string, returns null, otherwise returns an array where each item is an array as returned by match() for all matches of pattern in string.
replace_match(pattern, string, string|function(s))
replace all occurrences of the pattern in a string with either an expanded string (as explained here: https://pkg.go.dev/regexp#Regexp.Expand), or with the result of calling a function of 1 arg with the string that is to be replaced. The result of this function will not be expanded. Be aware that if you use a string substitution parameter with expansions, you need to quote the backslash character in order to protect it from abs' string interpolation.