title: Unclosed character class near index nnn
body: <p>I'm borrowing a rather complex regex from some PHP Textile implementations (open source, properly attributed) for a simple, not quite feature complete Java implementation, textile4j, that I'm porting to github and syncing to Maven central (the original code was written to provide a plugin for blojsom, a Java blogging platform; this is part of a larger effort to make blojsom dependencies available in Maven Central).</p>
  
  <p>Unfortunately, the textile regex expressions (while they work in context of <code>preg_replace_callback</code> in PHP) fail in Java with the following exception:</p>
  
  <p><code>java.util.regex.PatternSyntaxException: Unclosed character class near index 217</code></p>
  
  <p>The statement is obvious, the solution is elusive.</p>
  
  <p>Here's the raw, multiline regex from the PHP implementation:</p>
  
  <pre><code>return preg_replace_callback('/
      (^|(?&lt;=[\s&gt;.\(])|[{[]) # $pre
      "                      # start
      (' . $this-&gt;c . ')     # $atts
      ([^"]+?)               # $text
      (?:\(([^)]+?)\)(?="))? # $title
      ":
      ('.$this-&gt;urlch.'+?)   # $url
      (\/)?                  # $slash
      ([^\w\/;]*?)           # $post
      ([\]}]|(?=\s|$|\)))
      /x',callback,input);
  </code></pre>
  
  <p>Cleverly, I got the textile class to "show me the code" being used in this regex with a simple <code>echo</code> that resulted in the following, rather long, regular expression:</p>
  
  <pre><code>(^|(?&lt;=[\s&gt;.\(])|[{[])"((?:(?:\([^)]+\))|(?:\{[^}]+\})|(?:\[[^]]+\])|(?:\&lt;(?!&gt;)|(?&lt;!&lt;)\&gt;|\&lt;\&gt;|\=|[()]+(?! )))*)([^"]+?)(?:\(([^)]+?)\)(?="))?":([\w"$\-_.+!*'(),";\/?:@=&amp;%#{}|\^~\[\]`]+?)(\/)?([^\w\/;]*?)([\]}]|(?=\s|$|\)))
  </code></pre>
  
  <p>I've uncovered a couple of possible areas that could be resulting in parsing errors, using online tools such as <a href="http://gskinner.com/RegExr/">RegExr by gskinner</a> and <a href="http://www.regexplanet.com/">RegexPlanet</a>. However, none of those particulars fix the error.</p>
  
  <p><em>I suspect</em> that there is a range issue hidden in one of the character classes, or a Unicode order hiding somewhere, but I can't find it.</p>
  
  <p>Any ideas?</p>
  
  <p>I'm also curious why PHP doesn't throw a similar error, for example, I found one "passive subexpression" poorly handled using the RegExr, but it didn't fix the Java exception and didn't alter behavior in PHP, shown below.</p>
  
  <p>In <code>#title</code> switch the escaped paren:</p>
  
  <pre><code>        (?:\(([^)]+?)\)(?="))? # $title
          ...^
          (?:(\([^)]+?)\)(?="))? # $title
          ....^
  </code></pre>
  
  <p>Thanks,
  Tim</p>
  
  <p>edit: adding a Java String interpretation (with escapes) of the Textile regex, as determined by <a href="http://www.regexplanet.com/">RegexPlanet</a> ...</p>
  
  <pre><code>"(^|(?&lt;=[\\s&gt;.\\(])|[{[])\"((?:(?:\\([^)]+\\))|(?:\\{[^}]+\\})|(?:\\[[^]]+\\])|(?:\\&lt;(?!&gt;)|(?&lt;!&lt;)\\&gt;|\\&lt;\\&gt;|\\=|[()]+(?! )))*)([^\"]+?)(?:\\(([^)]+?)\\)(?=\"))?\":([\\w\"$\\-_.+!*'(),\";\\/?:@=&amp;%#{}|\\^~\\[\\]`]+?)(\\/)?([^\\w\\/;]*?)([\\]}]|(?=\\s|$|\\)))"
  </code></pre>