in reply to Replace every character that occurs more than one and their occurences are even

Based on the description given in the OP, almut has given the "right answer", but Corion has asked the right question, because the OP code seems to bear no relation to the description of the task, and it has some problems.

The s/// operation in your code contains only one set of parens in the regex match, but uses $2 in the replacement, which will always be an empty string. There's no mention of "XY" in the replacement. And given this type of regex pattern:

/(\w+)\1+/
strings that will match it include: "aa", "abab", "ababab", "abcabc", "abcabcabc", "abcdabcd", etc. -- that is, whenever any string of one or more letters is repeated one or more times -- but there will be quirks: "abababab" will be matched as "abab" occurring twice (not "ab" occurring four times).
  • Comment on Re: Replace every character that occurs more than one and their occurences are even
  • Download Code