title: Removing new lines/spaces from beginning and end of string Java
body: <p>I have a problem - I can't seem to be able to remove the new lines/spaces from the beginning/end of a string. I use <code>\s</code> in the beginning and end of the regex and even use <code>.trim()</code> after I get the string, but to no avail.</p>
  <pre><code>public void extractInfo(String mydata) {
  
      // regex to extract the user name
      Pattern pattern = Pattern.compile(&quot;user:\\s*(.*)\\s+branch&quot;);
      Matcher matcher = pattern.matcher(mydata);
      // regex to extract the branch name
      Pattern pattern2 = Pattern.compile(&quot;branch:\\s*(.*)\\s+changed&quot;);
      Matcher matcher2 = pattern2.matcher(mydata);
      // regex to extract the comment and write it in a variable
      comment = mydata.replaceAll(&quot;(?s)\\s.*java;[0-9,.]+|.*java;NONE\\s&quot;, &quot;&quot;);
  
      // put the author name in a variable
      matcher.find();
      author = matcher.group(1).toString();
  
      // put the branch name in a variable
      matcher2.find();
      branch = matcher2.group(1).toString();
  
      author.trim();
      comment.trim();
      branch.trim();
  }
  </code></pre>
  <p>This is what I use to extract the info.</p>
  <p>This is the output I get (lines kept), after I append the extracted information using StringBuilder:</p>
  <pre><code>git log --all -100 --before=&quot;2013-03-11&quot; --branches=HEAD
  --author=&quot;\(cholakov\)&quot; --grep=&quot;^[#]*[0]*23922:[ ]*user:
  </code></pre>
  <blockquote>
  <p>Fixed the message for defaulted bonds <code>&quot; --pretty=&quot;%H - %s ; %ad&quot;</code></p>
  </blockquote>
  <p><strong>The new line after <code>user:</code> is what causes the whole command to fail when I try to execute it in cmd, that's what I need fixed.</strong></p>
  <p>And this is my input (can't seem to be able to keep the formatting, DataObjectParser.java;1.94 is on a new line and there is no line skipped between each line):</p>
  <blockquote>
  <p>user: cholakov</p>
  <p>branch: HEAD</p>
  <p>changed files:
  DataObjectParser.java;1.94</p>
  <p>Fixed the message for defaulted bonds</p>
  </blockquote>