I need regular expression to replace all matching characters except the first one in a squence in string.
For example;
For matching with 'A' and replacing with 'B'
'AAA' should be replaced with 'ABB'
'AAA AAA' should be replaced with 'ABB ABB'
For matching with ' ' and replacing with 'X'
You need to use this regex for replacement:
\\BA
\B
(between word characters) assert position where \b
(word boundary) doesn't matchA
matches the character A
literallyJava Code:
String repl = input.replaceAll("\\BA", "B");
UPDATE For second part of your question use this regex for replacement:
"(?<!^|\\w) "
Code:
String repl = input.replaceAll("(?<!^|\\w) ", "X");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With