title: It is possible to pass a reference in a regex quantifier in Java?
body: <p>I had a programming exercise where we had to write a format method that would take as parameters a String, as the text input we want to format, and an integer, as the length of the lines we want. </p>
  
  <p>We also had specific rules, and one of them was: if a word is longer than the desired length, it should go on a line by itself. </p>
  
  <p>So an easy way to do this was to search with a Regex expression a series of non-white space characters being at least <em>length</em> and possibly more, and to replace this matched by itself with two \n at each side... </p>
  
  <p>perhaps the code is clearer: let's imagine our desired line length is 10</p>
  
  <pre><code>  String s = line.replaceAll("([ \t])(\\S{10,})([ \t])", "\n$2\n");
    System.out.println(s);
  </code></pre>
  
  <p>This actually works fine. </p>
  
  <p>My problem is that the line length should be passed as a parameter to the format method; so the only way I can refer to this parameter is by using its reference. For example:</p>
  
  <p>public static String format(String s, int <strong>length</strong>) { ...</p>
  
  <p>//missing stuff</p>
  
  <p>String s = line.replaceAll("([ \t])(\S{<strong>length</strong>,})([ \t])", "\n$2\n");
  System.out.println(s);</p>
  
  <p>However in that case it will return an error: PatternSyntaxException</p>
  
  <p>My question is: is there any way to use a reference into a quantifier (instead of a number), as argument for the number of occurrences we want to match? As it seems it would be really useful, I was surprised that it didn't seem to work. Also it doesn't look like a lot of people encountered this issue. </p>