java regular expression @path param optional

541 Views Asked by At

i need help with a java regular expression, I need to accept HTTP request at these paths:

categories
categories/533446dd2cdc5af82a027d9e,

533446dd2cdc5af82a027d9e stay for @Path param called "id";

I'm working on this regular expression:

categories(\/)?([a-z0-9]{0,25})?

but it's not working, can you help me? thanks guys :)

1

There are 1 best solutions below

0
On

Have a look at the following example :

String str1 = "categories/533446dd2cdc5af82a027d9e";
String str2 = "categories";
String pattern = "categories(/[a-z0-9]{0,24})?";
System.out.println(str1.matches(pattern));
System.out.println(str2.matches(pattern));

Output :

true
true