I can’t figure out how to create regular expression using positive lookahead. The idea is to prepend two character string to every two character in a long string. i.e.
"090909" => "XX09XX09XX09"
This code:
String s = "090909";
String ns = s.replaceAll("(?=\\d\\d)", "XX");
…doesn’t work; the output is XX0XX9XX0XX9XX09. But this code works:
String s = "090909";
String ns = s.replaceAll("(?=09)", "XX");
I’m confused on how to come up with an expression saying lookahead for every two characters. Am I missing some boundaries or something?
You can use the following:
The
(and)will create the capture, and the$1accesses the capture.