Map directive and regular expressions

Thanks for filling out the template!

Okay, I see what’s going on. I found it easy to replicate with this this config:

:8881 {

    map {path}      {my_placeholder}    {magic_number} {
        ~(/abc)     "ABC"               11
        ~(/xyz).*   "XYZ"               22

        default     "unknown"           42
    }

    header mapresult {my_placeholder}/{magic_number}
}

Then making requests like this:

$ curl -v localhost:8881/abcdef
< Mapresult: ABCdef/11def

$ curl -v localhost:8881/xyzdef
< Mapresult: XYZ/22

So basically, we’re using the Regexp.ReplaceAllString function to perform the replacements, and apparently the way it works is that if the match doesn’t completely cover the input string, then it appends the remainder of the input to the output.

By fully consuming the rest of the input with .*, then it has the desired result.

Yeah, you can use (?i) at the start of your regexp string to enable case insensitive mode. For example, ~(?i)(/xyz).* will match /xyz and /XYZ

2 Likes