I have searched a lot but I am unable to find a regex that could select only single alphabets and double them while those alphabets which are already double, should remain untouched.
I tried
String str = "yahoo"; str = str.replaceAll("(\w)\1+", "$0$0");
But since this (\w)\1+
selects all double elements, my output becomes yahoooo
. I tried to add negation to it !(\w)\1+
but didn’t work and output becomes same as input. I have tried
str.replaceAll(".", "$0$0");
But that doubles every character including which are already doubled.
Please help to write an regex
that could replace all single character with double while double character should remain untouched.
Example
abc -> aabbcc yahoo -> yyaahhoo (o should remain untouched) opinion -> ooppiinniioonn aaaaaabc -> aaaaaabbcc
Advertisement
Answer
You can match using this regex:
((.)2+)|(.)
And replace it with:
$1$3$3
RegEx Explanation:
((.)2+)
: Match a character and capture in group #2 and using2+
next to it to make sure we match all multiple repeats of captured character. Capture all the repeated characters in group #1|
: OR(.)
: Match any character and capture in group #3
import java.util.List; class Ideone { public static void main(String[] args) { List<String> input = List.of("aaa", "abc", "yahoo", "opinion", "aaaaaabc"); for (String s: input) { System.out.println( s + " => " + s.replaceAll("((.)\2+)|(.)", "$1$3$3") ); } } }
Output:
aaa => aaa abc => aabbcc yahoo => yyaahhoo opinion => ooppiinniioonn aaaaaabc => aaaaaabbcc