title: Golang Regex match and replace first occurrence after a certain string
body: <p>Following up on my previous <a href="https://stackoverflow.com/questions/54795852/golang-regex-replace-between-strings/54795889#54795889">question</a> about using Golang's regex to replace between strings. I now have a bit of complexity added to it. Here is what the contexts of my file looks like:</p>
  
  <pre><code>foo:
      blahblah
      MYSTRING=*
  bar:
      blah
      blah
      MYSTRING=*
  </code></pre>
  
  <p>I need to replace what's between <code>MYSTRING=</code> and <code>\n</code> with a string of my choice (like previous stated in the original post). I can do that with:</p>
  
  <pre><code>var re = regexp.MustCompile(`(MYSTRING=).*`)
  s := re.ReplaceAllString(content, `${1}stringofmychoice`)
  </code></pre>
  
  <p>But now I need to match and replace only after a certain occurrence. So that the contents of my file can look something like this:</p>
  
  <pre><code>foo:
      blahblah
      MYSTRING=foostring
  bar:
      blah
      blah
      MYSTRING=barstring
  </code></pre>
  
  <p><code>ReplaceAllString</code> obviously replaces everything, which is not what I want. Is there a way to only match and replace the first occurrence after a certain string?</p>
  
  <hr>
  
  <p>For a bit of background about all of this. I'm trying to write a program to edit the contents of a given <code>docker-compose.yml</code> file and its environment variables. I need to edit the environment variable <code>MYSTRING</code> differently depending on what service it's listed under. In the example above, the two different services would be <code>foo</code> and <code>bar</code>.</p>