title: Regex - matching leading and trailing spaces, spaces between opening and closing brackets and words, but not between words
body: <p>I apologize if this question has already been answered, but I have searched and cannot find the answer.  I am trying to write a regex that will match all leading and trailing space, the spaces between the opening and closing bracket and the word, but will not match the spaces between words. The following are string format examples of the data I'm parsing:</p>
  
  <pre><code>[Header]  
     [  SomeSpace]     
        [  Some1 More Space 15  ]       
  </code></pre>
  
  <ul>
  <li><p>no leading and trailing space, no space between brackets and only one word.</p></li>
  <li><p>some leading and trailing space, space between the opening bracket and trailing space.</p></li>
  <li><p>some leading space, space between word and digits, space between the opening and closing bracket, and trailing space. </p></li>
  </ul>
  
  <p>The closest single regex I've come up with is:  </p>
  
  <pre><code>/[^\[\]a-zA-Z\d]/
  </code></pre>
  
  <p>But I cannot seem to unmatch only the spaces between the words and digits... </p>
  
  <p>The ruby code I currently am using as a workaround is:</p>
  
  <pre><code>line.gsub!(/^\s*/, "")
  line.gsub!(/\[/, "")
  line.gsub!(/\]/, "")
  s = line.gsub!(/^\s*|\s*$/, "")
  s = "[" + s + "]\n"
  </code></pre>
  
  <p>Obviously, not very pretty... </p>
  
  <p>Any help to streamline this into an elegant gsub line is greatly appreciated.  </p>
  
  <p>Thanks!</p>
  
  <p>Lee</p>