-1

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.

reactor
  • 1,722
  • 1
  • 14
  • 34

1 Answers1

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