views:

140

answers:

3

hi,

i have this homework problem where i need to use regex to remove every other character in a string.

in one part, i have to delete characters at index 1,3,5,.... i have done this as follows:

String s = "1a2b3c4d5";
System.out.println(s.replaceAll("(.).", "$1"));

this prints 12345 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.

problem is i'm having trouble with the second part of the homework, where i need to delete characters at index 0,2,4,....

i have done the following:

String s = "1a2b3c4d5";
System.out.println(s.replaceAll(".(.)", "$1"));

this prints abcd5, but the correct answer must be abcd. my regex is only incorrect if the input string length is odd. if it's even, then my regex works fine.

i think i'm really close to the answer, but i'm not sure how to fix! please help!

+2  A: 

my regex is only incorrect if the input string length is odd. if it's even, then my regex works fine.

Change your expresion to .(.)? - the question mark makes the second character optional, which means it doesn't matter if input is odd or even

Matt
+14  A: 

You are indeed very close to the answer: just make matching the second char optional.

String s = "1a2b3c4d5";
System.out.println(s.replaceAll(".(.)?", "$1"));
// prints "abcd"

This works because:

  • Regex is greedy by default, it will take the second character if it's there
    • When the input is of odd length, the second char won't be there at the last replacement, but you'd still match one char (i.e. last char in input)
  • You can still use backreferences in substitution even if the group fails to match
    • It will substitute in the empty string, not "null"
    • This is different from Matcher.group(int), which returns null for failed groups

References


A closer look at the first part

Let's take a closer look at the first part of the homework:

String s = "1a2b3c4d5";
System.out.println(s.replaceAll("(.).", "$1"));
// prints "12345"

Here you didn't have to use ? for the second char, but it "works" because even though you didn't match the last char, you didn't have to! The last char can remain unmatched, unreplaced, due to the problem specification.

Now suppose that we want to delete chars at index 1,3,5..., and put the chars at index 0,2,4... in brackets.

String s = "1a2b3c4d5";
System.out.println(s.replaceAll("(.).", "($1)"));
// prints "(1)(2)(3)(4)5"

A-ha!! Now you're experiencing the exact same problem with odd-length input! You couldn't match the last char with your regex, because your regex needs two chars, but there's only one char at the end for odd-length input!

The solution, again, is to make matching the second char optional:

String s = "1a2b3c4d5";
System.out.println(s.replaceAll("(.).?", "($1)"));
// prints "(1)(2)(3)(4)(5)"
polygenelubricants
I haven't tested this, but does your first part of your homework work for both even and odd? You might need this fix on the first part too, for the same reason. I guess I'm not sure though.
glowcoder
@glowcoder: Great comment! I've delved deeper into the first part thanks to your remark!
polygenelubricants
A: 

Your regex needs 2 chars to match, so fails on the final char.

This regex:

".(.{0,1})"

Will make the second char optional, so it will match with your final '5' as well

tim_yates
Isn't `{0,1}` just a confusing, unnecessary way to write `?`
Matt
It's a different way to write it, but I'd argue with confusing and unnecessary
tim_yates
@tim: See also http://stackoverflow.com/questions/3032593/using-explicitly-numbered-repetition-instead-of-question-mark-star-and-plus
polygenelubricants