@Test public void test_single_quote_lookahead() { assertThat(quote(".ba.."), is("\\Q.ba..\\E")); assertThat(quote(".bar.."), is("\\.bar\\.\\.")); assertThat(quote("..bar."), is("\\Q..bar.\\E")); }
public static String quote(final String string) { int last = 0; final StringBuilder sb = new StringBuilder(); for (int i = 0; i < string.length(); i++) { if (needsQuoting(string.charAt(i))) { sb.append(string, last, i); last = i = quote(sb, string, i); } } sb.append(string, last, string.length()); return sb.toString(); }
RegEx { public static String quote(final String string) { int last = 0; final StringBuilder sb = new StringBuilder(); for (int i = 0; i < string.length(); i++) { if (needsQuoting(string.charAt(i))) { sb.append(string, last, i); last = i = quote(sb, string, i); } } sb.append(string, last, string.length()); return sb.toString(); } }
RegEx { public static String quote(final String string) { int last = 0; final StringBuilder sb = new StringBuilder(); for (int i = 0; i < string.length(); i++) { if (needsQuoting(string.charAt(i))) { sb.append(string, last, i); last = i = quote(sb, string, i); } } sb.append(string, last, string.length()); return sb.toString(); }  }
RegEx { public static String quote(final String string) { int last = 0; final StringBuilder sb = new StringBuilder(); for (int i = 0; i < string.length(); i++) { if (needsQuoting(string.charAt(i))) { sb.append(string, last, i); last = i = quote(sb, string, i); } } sb.append(string, last, string.length()); return sb.toString(); }  static String quote(final String string); }
RegEx { public static String quote(final String string) { int last = 0; final StringBuilder sb = new StringBuilder(); for (int i = 0; i < string.length(); i++) { if (needsQuoting(string.charAt(i))) { sb.append(string, last, i); last = i = quote(sb, string, i); } } sb.append(string, last, string.length()); return sb.toString(); }  static String quote(final String string);  }