title: Including comments in Java regular expressions
body: <p>I have some complex regular expressions which I need to comment for readability and maintenance. The Java spec is rather terse and I struggled for a long time getting this working. I finally caught my bug and will post it as an answer but I'd be grateful for any other advice on maintaining regexes</p>
  
  <p>As an example I want to comment the subcomponents (of patternS) in a simple name parser:</p>
  
  <pre><code>    String testTarget = "Waldorf T. Flywheel";
      String patternS = "([A-Za-z]+)\\s+([A-Z]\\.)?\\s+([A-Za-z]+)";
      Pattern pattern = Pattern.compile(patternS, Pattern.COMMENTS);
      Assert.assertTrue(pattern.matcher(testTarget).matches());
  </code></pre>
  
  <p>EDIT: I would be grateful for examples of the (?x) format as well. </p>
  
  <p>EDIT: @geowa4 has a good suggestion which avoids embedded comments. Sinnce java and others have provided for embedded comments what are the cases where they are useful? (I think I have a case but I'd be interested to see others).</p>
  
  <p>EDIT: As noted below @mikej the regex does not support the optional initial well and would be better as:</p>
  
  <pre><code>        String patternS = "([A-Za-z]+)\\s+([A-Z]\\.\\s+)?([A-Za-z]+)";
  </code></pre>
  
  <p>but that would end up extracting space in the initial</p>