The regexp package compiles and applies regular expressions in Go. It helps tests validate generated values without depending on unpredictable runtime identifiers. section 8.5.2
Finding an identifier in JSON
Compile `.+"id":"([a-zA-Z0-9]+)".+` with regexp.MustCompile, then call FindStringSubmatch. The captured value is returned separately, allowing the test to replace the generated ID before comparing JSON. section 8.5.2
Listing 8.37 newgame/handler_test.go: Replacing the ID in JSON output
// idFinderRegexp is a regular expression that will ensure the body
// contains an id field with a value that contains
// only letters (uppercase and/or lowercase) and/or digits.
idFinderRegexp := regexp.MustCompile(
`.+"id":"([a-zA-Z0-9]+)".+`) #1
id := idFinderRegexp.FindStringSubmatch(body) #2
if len(id) != 2 { #3
t.Fatal("cannot find one id in the json output")
}
body = strings.Replace(body, id[1], "123456", 1) #4
assert.JSONEq(t, testCase.wantBody, body) #5
Shows compiled-pattern matching, capture extraction, length checking, and deterministic replacement in a handler test. section 8.5.2

