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 match
  • a matches character a 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"); 

demo 2


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -