Is this the proper REGEX to remove trailing decimal and zeroes from a string? I can’t get it to work. What am I missing?
- 78.000 -> 78
- 78.008 -> 78.008
str.replaceAll("^.0*$", "");
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You need to escape the
., as it is a special character in Regex that matches any character. You also have to remove the^, which anchors at the beginning of the number.You can use a lookbehind if you want to make sure there is a number in front of the dot, like this:
The lookbehind (the
(?<=...)part) is not a part of the match, so it will not be replaced, but it still has to match for the rest of the regex to match.