java - Regular Expression to Replace All But One Character In String -
i need regular expression replace matching characters except first 1 in squence in string.
for example;
for matching 'a' , replacing 'b'
'aaa' should replaced 'abb'
'aaa aaa' should replaced 'abb abb'
for matching ' ' , replacing 'x'
- '[space][space][space]a[space][space][space]' should replaced '[space]xxa[space]xx'
you need use regex replacement:
\\ba
working demo
\b
(between word characters) assert position\b
(word boundary) doesn't matcha
matches charactera
literally
java code:
string repl = input.replaceall("\\ba", "b");
update second part of question use regex replacement:
"(?<!^|\\w) "
code:
string repl = input.replaceall("(?<!^|\\w) ", "x");
Comments
Post a Comment