title: Replace comma with a comma followed by space
body: <p>Due to the way server-side data is formatted I need to reformat it with my own function and to prevent any unwanted reformatting I want to check if the string I'm reformatting needs to be reformatted.</p>
  
  <p>Here is the condition and the regex involved.</p>
  
  <pre><code>String value = "test, test, test.";
  
  if (!value.matches("/(\\s*, )/g")) {
      return value.replaceAll(",", ", ");
  }
  else {
      return value;
  }
  </code></pre>
  
  <p>The expected result is if a string is: <code>Test,test,test</code> it will reformat it to <code>Test, test, test</code>.</p>
  
  <p>The behavior I am getting is that even the <code>value</code> string in the code goes into the <code>if</code> condition when it should skip it and go into the <code>else</code>.</p>
  
  <h2>Edit after consulting with my reviewer</h2>
  
  <p>The best solution found here for me was <code>",(?! )", ", "</code> but it solved a problem I didn't even have so I simplified it to <code>",(\\S)", ", $1"</code>. </p>
  
  <p>The proposed solution would add a <code></code> after <code>,</code> even if the <code>,</code> would be at the end of the string which is something I didn't want.</p>
  
  <p>Also in my question, I had a redundant <code>if</code> condition which I removed and simplified the solution to:</p>
  
  <pre><code>String value = "Test,test,test";
  
  return value.replaceAll(",(\\S)", ", $1");
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <p><code>Test, test, test</code></p>