I have this regex in my code:
get.HandleFunc("/my/api/user/{userID:^([1-9][0-9]*)$}", app.Handle("user"))
But when I run the tests, only 404s are returned. I also tried this:
get.HandleFunc("/my/api/user/{userID:\\A([1-9][0-9]*)\\z}", app.Handle("user"))
It worked perfectly with my older (but incorrect) regex:
get.HandleFunc("/my/api/user/{userID:[0-9]{1,}}", app.Handle("user"))
I wonder what is wrong with my new regex. I tried to test it on some websites and also by using the regexp
package in Go, and it always worked. As far as I know, gorilla/mux
uses Go's regexp
package. Any idea?
What I want is to detect positive integers excluding zero.
The anchors are most likely the issue here, you're trying to assert at start/end of string positions.
I would simply try modifying the older (working) regular expression as follows:
get.HandleFunc("/my/api/user/{userID:[1-9][0-9]*}", app.Handle("user"))
If you want to detect positive integers excluding 0, you should use this characters class: [1-9]\d*
That means, the first digit must be between 1 and 9. The other digits (if present, look at the *
) can be any integer, 0 included.