riff
riff copied to clipboard
Implement variable/expression interpolation in regex literals
Similar to Riff's string interpolation introduced in version 0.3.5, the same can be done for regex literals (with a different sigil). This would require conditionally compiling some regexes as runtime.
$...
would be available, making regex literals resemble Perl very closely. Though it's not clear how something like $1
should be handled.
Note that either this feature or some kind of builtin function (regex(pattern[,flags])
?) needs to exist to provide full capability of dynamically-created regexes. While regexes can be implicitly created at runtime by using strings with match operations, there's no way to specify regex compilation flags.
One benefit of a regex()
function would be that compilation flags can also be dynamic. So it may be nice to have the builtin regardless. The choice of sigil for string interpolation (#
) conflicts with the PCRE2 comment symbol for extended regexes (e.g. /pattern #comment/x
), so a regex()
function still doesn't fully work here unless the string interpolation sigil changes ($
can't be used for strings since [g]sub()
utilizes the default "dollar escape" feature of pcre2_substitute()
).
~~If the interpolation sigils were the same, strings and regexes could converge to a single data type; lazily compiling and caching a regex object when used on a matching operation. That would also make the disassembly a little simpler. You could define a regex as a string literal with postfix flags (e.g. 'foo $bar'i
).~~ This is incorrect for a variety of reasons.