I'd like to trim any single trailing . or -. I tried doing this by doing something like "f-o.o.".replaceFirst("^(\\.+)[-|.]$", "$0"). The expected string is f-o.o but I'm getting f-o.o.. Thank you.
Asked
Active
Viewed 122 times
-1
reactor
- 1,722
- 1
- 14
- 34
-
2This might answer your question: https://stackoverflow.com/questions/25691415/java-trim-leading-or-trailing-characters-from-a-string – R.Groote Feb 28 '20 at 06:46
-
1It is `.replaceFirst("[.-]\\z", "")` – Wiktor Stribiżew Feb 28 '20 at 06:58
1 Answers
2
Your expression has two mistakes:
you put a slash in front of a dot, making it match a literal dot, not just any character
you put
|into a character class, so your expression would remove not just.or-at the end of the string, but also|.
Use "f-o.o.".replaceFirst("[-.]$", "")
Alex Sveshnikov
- 4,214
- 1
- 10
- 26