Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, you use pattern matching where you don’t have a <em>pattern</em>. This is not only wasting performance, it is an error. For <code>"://www."</code> used as a pattern the last dot matches <em>any character</em>.</p> <p>If you want to replace a simple <code>String</code> rather than a pattern, you can use <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace%28java.lang.CharSequence,%20java.lang.CharSequence%29" rel="nofollow"><code>String.replace</code></a> instead of <code>String.replaceAll</code>. This fixes the error and saves the entire overhead of creating a <code>Pattern</code> and a <code>Matcher</code> internally.</p> <p>If you want to use the regex matcher, the simplest fix is to use <code>Pattern.compile("://www.", Pattern.LITERAL)</code> to stop interpreting special characters and allow using the Boyer–Moore algorithm for the entire sequence. This, however, will only pay off if you can store and re-use the prepared pattern.</p> <p>Further, the <code>replaceAll</code> method offers the feature of interpreting back-references to groups of the match in the replacement <code>String</code>. Since you’re not using this feature, you can save the overhead associated with that feature by implementing the replacement loop by yourself. As a bonus you can use <code>StringBuilder</code> where the <code>Matcher</code> uses <code>StringBuffer</code>, but that’s only a minor improvement.</p> <pre><code>static String replace(String source) {// the pattern would be better off being re-usable stored in a static field final Pattern pattern = Pattern.compile("://www.", Pattern.LITERAL); final Matcher m = pattern.matcher(source); boolean result = m.find(); if (result) { StringBuilder sb = new StringBuilder(source.length()+16); int p=0; do { sb.append(source, p, m.start()).append("://www-x1."); p=m.end(); } while (m.find()); sb.append(source, p, source.length()); return sb.toString(); } return source; } </code></pre> <p>But as already said, not using regex at all might be the better choice for this simple case here.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload