java - String.replaceAll() with [\d]* appends replacement String inbetween characters, why? -
i have been trying hours regex statement match unknown quantity of consecutive numbers. believe [0-9]* or [\d]* should want yet when use java's string.replaceall adds replacement string in places shouldn't matching regex.
for example: have input string of "this my99string problem" if replacement string "~"
when run this
mystring.replaceall("[\\d]*", "~" )
or
mystring.replaceall("[0-9]*", "~" )
my return string "~t~h~i~s~ ~i~s~ ~m~y~~s~t~r~i~n~g~ ~p~r~o~b~l~e~m~"
as can see numbers have been replaced why appending replacement string in between characters.
i want "this my~string problem"
what doing wrong , why java matching this.
\\d*
matches 0 or more digits, , matches empty string. , have empty string before every character in string. so, each of them, replaces ~
, hence result.
try using \\d+
instead. , don't need include \\d
in character class.
Comments
Post a Comment