From: Alan T. DeKok Date: Tue, 29 Apr 2025 14:29:18 +0000 (-0400) Subject: %regex() -> %regex.match() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=515a0f4a420055dfbe404719844af9e6a1861dee;p=thirdparty%2Ffreeradius-server.git %regex() -> %regex.match() --- diff --git a/doc/antora/modules/reference/pages/unlang/condition/regex.adoc b/doc/antora/modules/reference/pages/unlang/condition/regex.adoc index a9bcaac708..848370c97f 100644 --- a/doc/antora/modules/reference/pages/unlang/condition/regex.adoc +++ b/doc/antora/modules/reference/pages/unlang/condition/regex.adoc @@ -119,7 +119,7 @@ matched. The expansions + When using libpcre[2], named capture groups may also be accessed using the built-in expansion + -`%regex()`. +`%regex.match()`. Please see the xref:xlat/builtin.adoc#_0_32[xlat documentation] for more information on regular expression matching. diff --git a/doc/antora/modules/reference/pages/xlat/builtin.adoc b/doc/antora/modules/reference/pages/xlat/builtin.adoc index 31bd919b11..80a205624a 100644 --- a/doc/antora/modules/reference/pages/xlat/builtin.adoc +++ b/doc/antora/modules/reference/pages/xlat/builtin.adoc @@ -92,11 +92,11 @@ groups in the pattern. Every time a regular expression is evaluated, whether it matches or not, the numbered capture group values will be cleared. -=== +%regex(}+ +=== +%regex.match(}+ Return named subcapture value from the last regular expression evaluated. -Results of named capture groups are available using the `%regex(}` expansion. They will also be accessible using the numbered expansions described xref:xlat/builtin.adoc#_0_32[above]. diff --git a/src/lib/unlang/xlat_builtin.c b/src/lib/unlang/xlat_builtin.c index 87055dd5ff..00a85488d9 100644 --- a/src/lib/unlang/xlat_builtin.c +++ b/src/lib/unlang/xlat_builtin.c @@ -2792,7 +2792,7 @@ static xlat_arg_parser_t const xlat_func_regex_args[] = { if ("foo" =~ /^(?.*)/) { noop } -%regex(name) == "foo" +%regex.match(name) == "foo" @endverbatim * * @ingroup xlat_functions @@ -4333,9 +4333,13 @@ do { \ XLAT_NEW("hash.md4"); #if defined(HAVE_REGEX_PCRE) || defined(HAVE_REGEX_PCRE2) + if (unlikely((xlat = xlat_func_register(xlat_ctx, "regex.match", xlat_func_regex, FR_TYPE_STRING)) == NULL)) return -1; + xlat_func_args_set(xlat, xlat_func_regex_args); + xlat_func_flags_set(xlat, XLAT_FUNC_FLAG_INTERNAL); if (unlikely((xlat = xlat_func_register(xlat_ctx, "regex", xlat_func_regex, FR_TYPE_STRING)) == NULL)) return -1; xlat_func_args_set(xlat, xlat_func_regex_args); xlat_func_flags_set(xlat, XLAT_FUNC_FLAG_INTERNAL); + XLAT_NEW("regex.match"); #endif { diff --git a/src/tests/keywords/if-failed-xlat b/src/tests/keywords/if-failed-xlat index 420b55d9bf..495d8bed59 100644 --- a/src/tests/keywords/if-failed-xlat +++ b/src/tests/keywords/if-failed-xlat @@ -6,7 +6,7 @@ if (('${feature.regex-pcre}' == 'yes') || ('${feature.regex-pcre2}' == 'yes')) { # Check failure when no previous capture - named group # but a failed regex is equivalent to an empty string -if (%regex('foo')) { +if (%regex.match('foo')) { test_fail } diff --git a/src/tests/keywords/if-regex-match-named b/src/tests/keywords/if-regex-match-named index 714f4e8f8a..e86a52a036 100644 --- a/src/tests/keywords/if-regex-match-named +++ b/src/tests/keywords/if-regex-match-named @@ -10,7 +10,7 @@ if (('${feature.regex-pcre}' == 'yes') || ('${feature.regex-pcre2}' == 'yes')) { } # Check failure when no previous capture - full capture -if (%regex()) { +if (%regex.match()) { test_fail } @@ -21,7 +21,7 @@ if (!(Module-Failure-Message[*] == "No previous regex capture")) { request -= Module-Failure-Message[*] # Check failure when no previous capture - named group -if (%regex('foo')) { +if (%regex.match('foo')) { test_fail } @@ -32,7 +32,7 @@ if (!(Module-Failure-Message[*] == "No previous named regex capture group 'foo'" request -= Module-Failure-Message[*] # Check failure when no previous capture - numbered group -if (%regex(Port-Limit)) { +if (%regex.match(Port-Limit)) { test_fail } @@ -44,7 +44,7 @@ request -= Module-Failure-Message[*] # uncompiled - ref - named capture groups if (User-Name =~ /^(?[0-9])_(?[0-9])?_(?[0-9]*)_(?[0-9]+)_(?[^_])_(?6)_(?[7-8])%{dummy_string}/) { - result_string := "%regex('seven')_%regex('six')_%regex('five')_%regex('four')_%regex('three')_%regex('two')_%regex('one')_%{0}" + result_string := "%regex.match('seven')_%regex.match('six')_%regex.match('five')_%regex.match('four')_%regex.match('three')_%regex.match('two')_%regex.match('one')_%{0}" if (!(result_string == '7_6_5_4_3_2_1_1_2_3_4_5_6_7')) { test_fail } @@ -55,7 +55,7 @@ else { # Checking capture groups are cleared out correctly if (User-Name =~ /^(?[0-9])_%{dummy_string}/) { - result_string := "%{0}%regex('one')%regex('two')%regex('three')%regex('four')%regex('five')%regex('six')%regex('seven')" + result_string := "%{0}%regex.match('one')%regex.match('two')%regex.match('three')%regex.match('four')%regex.match('five')%regex.match('six')%regex.match('seven')" if (!(result_string == '1_1')) { test_fail } @@ -66,7 +66,7 @@ else { # Checking capture groups are cleared out correctly when there are no matches if (User-Name =~ /^.%{dummy_string}/) { - result_string := "%{0}%regex('one')%regex('two')%regex('three')%regex('four')%regex('five')%regex('six')%regex('seven')" + result_string := "%{0}%regex.match('one')%regex.match('two')%regex.match('three')%regex.match('four')%regex.match('five')%regex.match('six')%regex.match('seven')" if (!(result_string == '1')) { test_fail } @@ -77,7 +77,7 @@ else { # compiled - ref - named capture groups if (User-Name =~ /^(?[0-9])_(?[0-9])?_(?[0-9]*)_(?[0-9]+)_(?[^_])_(?6)_(?[7-8])/) { - result_string := "%regex('seven')_%regex('six')_%regex('five')_%regex('four')_%regex('three')_%regex('two')_%regex('one')_%{0}" + result_string := "%regex.match('seven')_%regex.match('six')_%regex.match('five')_%regex.match('four')_%regex.match('three')_%regex.match('two')_%regex.match('one')_%{0}" if (!(result_string == '7_6_5_4_3_2_1_1_2_3_4_5_6_7')) { test_fail } @@ -88,7 +88,7 @@ else { # compiled - xlat - named capture groups if ('1_2_3_4_5_6_7' =~ /^(?[0-9])_(?[0-9])?_(?[0-9]*)_(?[0-9]+)_(?[^_])_(?6)_(?[7-8])/) { - result_string := "%regex('seven')_%regex('six')_%regex('five')_%regex('four')_%regex('three')_%regex('two')_%regex('one')_%{0}" + result_string := "%regex.match('seven')_%regex.match('six')_%regex.match('five')_%regex.match('four')_%regex.match('three')_%regex.match('two')_%regex.match('one')_%{0}" if (!(result_string == '7_6_5_4_3_2_1_1_2_3_4_5_6_7')) { test_fail } @@ -128,7 +128,7 @@ if (User-Name =~ /^(?[0-9])_(?[0-9])?_(?[0-9]*)_(?[0-9]+) g := 6 h := 7 - result_string := "%regex(h)_%regex(g)_%regex(f)_%regex(e)_%regex(d)_%regex(c)_%regex(b)_%regex(a)" + result_string := "%regex.match(h)_%regex.match(g)_%regex.match(f)_%regex.match(e)_%regex.match(d)_%regex.match(c)_%regex.match(b)_%regex.match(a)" if (!(result_string == '7_6_5_4_3_2_1_1_2_3_4_5_6_7')) { test_fail }