-1

I have this image copy method that uses the name of the image being copied for the new version. If I am copying the same image often it will add (integer) behind the image name: like image.jpg becomes image(1).jpg. Now I am trying to use regex to replace the (int) part but I am not quite getting how to do it.

imageName.replaceAll(["(]\\d[)]","");

But does this only delete (1) but not if there were like two digits (12)?

Delpux
  • 137
  • 4
  • Your quotes are a bit off (after your opening bracket instead of ahead), but are you looking for: `\\(\\d+\\)`??. Not a `Java` guy, so might be wrong on the double backslashes... – JvdV Apr 24 '20 at 07:36

2 Answers2

1
"image(1).jpg".replaceAll("\\(\\d+\\)", "")

RegExr demo

The key bit to this is the + operator which matches one or more element of the preceding expression.

simon-pearson
  • 1,601
  • 8
  • 10
1

You can use for the same imageName.replaceAll("[(]\d*\d[)]", "");

SSK
  • 3,444
  • 6
  • 32
  • 59