-1

I have to remove some parts of a string like this:

customers/00000000-0000-0000-0000-000000000111/areas/00000000-0000-0000-0000-000000000222/orders/00000000-0000-0000-0000-000000000555/invoices/00000000-0000-0000-0000-000000000777/employees/00000000-0000-0000-0000-000000000213.gz

To get the path without the uuids but with the file extension. It should look like

customers/areas/orders/invoices/employees/.gz

How can I do this? With Matcher or substring? Which is best option? Is there any custom regex that I can use to remove UUIDs from the string?

Adam Jungen
  • 307
  • 2
  • 5
  • 19

3 Answers3

1

You can replace the string using the following Regex:

(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}
Tig7r
  • 287
  • 1
  • 3
  • 17
1

Assuming that UUID in your case are only numbers with minus You can use a regular expression like the following

[0-9][-0-9]*[0-9][\/]?

to replace strings like

00000000-0000-0000-0000-000000000111/
00000000-0000-0000-0000-000000000222/
00000000-0000-0000-0000-000000000555/
00000000-0000-0000-0000-000000000213
...

with an empty string as in the following code:

String path = ...;
// Note the additional \ in the following string, 
// this is necessary because we are using a string to represent the regexp
System.out.println(path.replaceAll("[0-9][-0-9]*[0-9][\\/]?", ""));

that prints

customers/areas/orders/invoices/employees/.gz
Davide Lorenzo MARINO
  • 22,769
  • 4
  • 33
  • 48
0

You could give a try FilenameUtils.getExtension from Apache Commons IO

(you may specify either full path or just file name):

String ext1 = FilenameUtils.getExtension("customers/00000000-0000-0000-0000-000000000111/areas/00000000-0000-0000-0000-000000000222/orders/00000000-0000-0000-0000-000000000555/invoices/00000000-0000-0000-0000-000000000777/employees/00000000-0000-0000-0000-000000000213.gz"); // returns "gz"
String ext2 = FilenameUtils.getExtension("002345620000000213.txt"); // returns "txt"
Vaibhav_Sharma
  • 526
  • 1
  • 7
  • 19