title: sed - regex square brackets detection in Linux
body: <p>I am using Ubuntu 14.04, and I have the following statement: </p>
  
  <pre><code>192.168.2.4 [text to capture] Test: This is a test statement.
  </code></pre>
  
  <p>I am trying to capture "text to capture" using the following regex: </p>
  
  <pre><code>echo "192.168.2.4 [text to capture] Test: This is a test statement" | sed -r "s/^[^\[\]]*\[(.*)\].*$/\1/"
  </code></pre>
  
  <p>The idea behind the regex is to traverse over all characters which do not match opening and closing square bracket. Once an opening square bracket is encountered, capture the text until the closing bracket is encountered, then ignore all subsequent characters.</p>
  
  <p>When I use the regex above in a <a href="https://regex101.com/#pcre" rel="nofollow">regex tester</a>, I can see that the "text to capture" is being captured. </p>
  
  <p>However, executing the regex command above returns the complete statement aka: </p>
  
  <pre><code>echo "192.168.2.4 [text to capture] Test: This is a test statement" | sed -r "s/^[^\[\]]*\[(.*)\].*$/\1/"
  </code></pre>
  
  <p>Can anyone spot what I have missed here? I believe I have escaped the characters brackets correctly, since it is working correctly with the regex tester.</p>
  
  <p>Thanks
  John</p>