title: Removing every other character in a string using Java regex
body: <p>I have this homework problem where I need to use regex to remove every other character in a string.</p>
  
  <p>In one part, I have to delete characters at index 1,3,5,... I have done this as follows:</p>
  
  <pre><code>String s = "1a2b3c4d5";
  System.out.println(s.replaceAll("(.).", "$1"));
  </code></pre>
  
  <p>This prints <code>12345</code> which is what I want. Essentially I match two characters at a time, and replacing with the first character. I used group capturing to do this. </p>
  
  <p>The problem is, I'm having trouble with the second part of the homework, where I need to delete characters at index 0,2,4,...</p>
  
  <p>I have done the following:</p>
  
  <pre><code>String s = "1a2b3c4d5";
  System.out.println(s.replaceAll(".(.)", "$1"));
  </code></pre>
  
  <p>This prints <code>abcd5</code>, but the correct answer must be <code>abcd</code>. My regex is only incorrect if the input string length is odd. If it's even, then my regex works fine.</p>
  
  <p>I think I'm really close to the answer, but I'm not sure how to fix it.</p>