Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#297·miller

Match regex metacharacters: remaining step now is r-strings

Author: torbiakCreated Jan 6, 2020Updated Jul 15, 2026
Labelsactivefeature-requestplan-written

Very similar to #159, I can't match a square bracket, asterisk, etc, regardless how many backslashes I put because mlr_alloc_double_backslash is doubling them. Adding exceptions for square brackets, like the one already there for period, allows me to match them as expected, but it seems we'd either need an exception for all the ERE metacharacters or take a different approach to escaping backslashes.

$ echo 'a=[' | c/mlr put '$a = gsub($a, "\[", "left_square")'
mlr: could not compile regex "\[" : Invalid regular expression
$ git stash pop
$ git diff
diff --git a/c/lib/mlrutil.c b/c/lib/mlrutil.c
index a532a25e..dfd86e18 100644
--- a/c/lib/mlrutil.c
+++ b/c/lib/mlrutil.c
@@ -502,7 +502,7 @@ char* mlr_alloc_double_backslash(char* input) {
        char* output = mlr_malloc_or_die(input_length + num_backslashes + 1);
        for (p = input, q = output; *p; p++) {
                if (*p == '\\') {
-                       if (p[1] != '.') {
+                       if (p[1] != '.' && p[1] != '[' && p[1] != ']') {
                                *(q++) = *p;
                        }
                        *(q++) = *p;
$ make
$ echo 'a=[' | c/mlr put '$a = gsub($a, "\[", "left_square")'
a=left_square

Source: johnkerl/miller

View original on GitHubView discussion on GitHub