title: Regex for converting CamelCase to camel_case in java
body: <p>I understand why the desired output is not given for converting using regex a string like <code>FooBar</code> to <code>Foo_Bar</code> which instead gives <code>Foo_Bar_</code>. I could have done something with String.substring <code>substring(0, string.length() - 2)</code> or just replaced the last character, but I think there is a better solution to such a scenario.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>String regex = "([A-Z][a-z]+)";
  String replacement = "$1_";
  
  "CamelCaseToSomethingElse".replaceAll(regex, replacement); 
  
  /*
  outputs: Camel_Case_To_Something_Else_
  desired output: Camel_Case_To_Something_Else
  */
  </code></pre>
  
  <p>Question: Looking for a neater way to get the desired output?</p>