Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Given your description, I'm assuming that after the <code>NNDDDDD</code> portion, the first <code>A</code> will actually be a <code>N</code> rather than an <code>A</code>, since otherwise there's no solid boundary between the <code>DDDDD</code> and <code>AAAA</code> portions.</p> <p>So, your string actually looks like <code>NNDDDDDNAAA</code>, and you want to replace the <code>NAAA</code> portion with spaces. Given this, the regex can be rewritten as such: <code>(\\D+\\d+)(\\D.+)</code></p> <p>Positive lookbehind in Java requires a fixed length pattern; You can't use the <code>+</code> or <code>*</code> patterns. You can instead use the curly braces and specify a maximum length. For instance, you can use <code>{1,9}</code> in place of each <code>+</code>, and it will match between 1 and 9 characters: <code>(?&lt;=\\D{1,9}\\d{1,9})(\\D.+)</code></p> <p>The only problem here is you're matching the NAAA sequence as a single match, so using <code>"NNNDDDDNAAA".replaceAll("(?&lt;=\\D{1,9}\\d{1,9})(\\D.+)", " ")</code> will result in replacing the entire <code>NAAA</code> sequence with a single space, rather than multiple spaces.</p> <p>You could take the beginning delimiter of the match, and the string length, and use that to append the correct number of spaces, but I don't see the point. I think you're better off with your original solution; Its simple and easy to follow.</p> <p>If you're looking for a little extra speed, you could compile your Pattern outside the function, and use StringBuilder or StringBuffer to create your output. If you're building a large String out of all these NNDDDDDAAAAA elements, work entirely in StringBuilder until you're done appending.</p> <pre><code>class Test { public static Pattern p = Pattern.compile("(\\D+\\d+)(\\D.+)"); public static StringBuffer replace( String input ) { StringBuffer output = new StringBuffer(); Matcher m = Test.p.matcher(input); if( m.matches() ) output.append( m.group(1) ).append( m.group(2).replaceAll("."," ") ); return output; } public static void main( String[] args ) { String input = args[0]; long startTime; StringBuffer tests = new StringBuffer(); startTime = System.currentTimeMillis(); for( int i = 0; i &lt; 50; i++) { tests.append( "Input -&gt; Output: '" ); tests.append( input ); tests.append( "' -&gt; '" ); tests.append( Test.replace( input ) ); tests.append( "'\n" ); } System.out.println( tests.toString() ); System.out.println( "\n" + (System.currentTimeMillis()-startTime)); } } </code></pre> <p><strong>Update:</strong> I wrote a quick iterative solution, and ran some random data through both. The iterative solution is around 4-5x faster.</p> <pre><code>public static StringBuffer replace( String input ) { StringBuffer output = new StringBuffer(); boolean second = false, third = false; for( int i = 0; i &lt; input.length(); i++ ) { if( !second &amp;&amp; Character.isDigit(input.charAt(i)) ) second = true; if( second &amp;&amp; !third &amp;&amp; Character.isLetter(input.charAt(i)) ) third = true; if( second &amp;&amp; third ) output.append( ' ' ); else output.append( input.charAt(i) ); } return output; } </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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