title: How to Extract sub-string after delimiter but ignore if found between two tags?
body: <p>I need to extract a sub-string after a specific delimiter, but if the specified delimiter is between two other tags it should be ignored.</p>
  
  <p>For example, take this test string:</p>
  
  <blockquote>
    <p>The quick &lt;"@brown fox"> jumps over the lazy dog. The quick @<em>brown fox
    jumps over the lazy dog</em></p>
  </blockquote>
  
  <p>The desired output would be:</p>
  
  <blockquote>
    <p><em>brown fox jumps over the lazy dog</em></p>
  </blockquote>
  
  <p>This is because the first found @ delimiter is between two " " and so should be ignored, the second @ delimiter is not inside " " and so the text afterwards should be extracted.</p>
  
  <p>I am able to find the starting position of the @ delimiter by using <code>Pos</code> and extracting the text to the right of it as shown below:</p>
  
  <pre><code>procedure TForm1.Button1Click(Sender: TObject);
  var
    S: string;
    I: Integer;
  begin
    S := 'The quick &lt;"@brown fox"&gt; jumps over the lazy dog. The quick @brown fox jumps over the lazy dog';
    I := Pos('@', S);
    if I &gt; 0 then
    begin
      ShowMessage(Copy(S, I, Length(S)));
    end;
  end;
  </code></pre>
  
  <p>However this will always find the first @ delimiter regardless if it is surrounded by two " " or not. The result from the above is:</p>
  
  <blockquote>
    <p>@brown fox"> jumps over the lazy dog. The quick @<em>brown fox jumps over
    the lazy dog</em></p>
  </blockquote>
  
  <p>where the desired result should be:</p>
  
  <blockquote>
    <p><em>brown fox jumps over the lazy dog</em></p>
  </blockquote>
  
  <p>How can I change the code to ignore @ delimiters when using <code>Pos</code> if the delimiter is between two " " tags? I only want to find the first @ delimiter and copy the text afterwards.</p>
  
  <p>It also does not matter if there are any other @ delimiters after the first valid one is found, for example this should also be valid:</p>
  
  <blockquote>
    <p>The quick &lt;"@brown fox"> jumps over the lazy dog. The quick @<em>brown fox
    jumps@ ov@er the lazy@ dog</em></p>
  </blockquote>
  
  <p>Should still return:</p>
  
  <blockquote>
    <p><em>brown fox jumps over the lazy dog</em></p>
  </blockquote>
  
  <p>Because we are only interested in the first valid @ delimiter, ignoring anything else afterwards and ignoring anything between two " " tags.</p>
  
  <p>Please note although I have tagged Delphi I do primarily use Lazarus so ideally I would need help coming up with a solution that does not use magic help with string helpers etc.</p>
  
  <p>Thanks.</p>