在io.ReadCloser中查找字符串而无需进行大量分配

I have a large io.ReadCloser that I got from an http.Request in my HTTP handler func. I need to proxy the request to another server, but first I want to find a string in the body matching a regex like Title: (\w+). This is hard -- copying the whole body into a new buffer to operate on takes up way too much memory, and I've tried using regexp.FindReaderSubmatchIndex but it only gives me the index of the result, not the actual string.

What's the best way to do this? Tokenizers and JSON decoders and such seem to work on io streams, and this is a really simple use case for that. Can someone please point me in the right direction?

Here's my solution. I placed a pipe between the response body and its reader, and wrapped the reader with an io.TeeReader so it would write to the pipe as I read from it. I wrapped that in a bufio.Scanner and scanned lines. After I was done scanning lines, I was sure to consume the remainder of the body (with io.Copy(ioutil.Discard, body)) so that the rest of the body would be written to the pipe.

if request.Body == nil {
    proxy(request)
    return
}

// The body is *not* nil,
// so we're going to process it line-by-line.

bodySrc := request.Body             // Original io source of the request body.
pr, pw := io.Pipe()                 // Pipe between bodySrc and request.Body.
body := io.TeeReader(bodySrc, pw)   // When you read from body, it will read from bodySrc and writes to the pipe.
request.Body = ioutil.NopCloser(pr) // The other end of the pipe is request.Body. That's what proxy() will read.

go func() {

    scanner = bufio.NewScanner(body)
    for scanner.Scan() {
        x := scanner.Bytes()
        if processLine(x) {
            break
        }
    }

    // We're done with the body,
    // so consume the rest of it and close the source and the pipe.
    io.Copy(ioutil.Discard, body)
    bodySrc.Close()
    pw.Close()

}()

// As proxy reads request.Body, it's actually keeping up
// with the scanning done in the above goroutine.
proxy(request)

I would use io.TeeReader for that and pass special writer to the TeeReader constructor. Consider following as a pseudocode because there are some edge cases that we don't handle here:

package main

import (
    "fmt"
    "io"
    "io/ioutil"
    "net/http"
    "regexp"
)

type Finder struct {
    Regexp *regexp.Regexp
    match  string
}

//Write implements io.Writer interface
func (f *Finder) Write(p []byte) (int, error) {
    if f.match == "" {
        f.match = string(f.Regexp.Find(p))
    }

    return len(p), nil
}

func Handler(w http.ResponseWriter, r *http.Request) {
    f := &Finder{
        Regexp: regexp.MustCompile("Title: ([a-zA-Z0-9]+)"),
    }

    r.Body = ioutil.NopCloser(io.TeeReader(r.Body, f))

    //pass request to another server

    fmt.Println(f.match)
}