The problem with your regex is that you allow / at least 2 times with /? .
You need to allow only / once.
^[A-Za-z0-9]+/?[A-Za-z0-9]*$
In addition, matches requires complete string matching, no ^ and $ bindings are needed in this regular expression if you plan to use it only with matches .
Watch the IDEONE demo
System.out.println("ABC321".matches("[A-Za-z0-9]+/?[A-Za-z0-9]*")); System.out.println("ABC321/OTHER888".matches("[A-Za-z0-9]+/?[A-Za-z0-9]*")); System.out.println("ABC321/".matches("[A-Za-z0-9]+/?[A-Za-z0-9]*")); System.out.println("/ABC321".matches("[A-Za-z0-9]+/?[A-Za-z0-9]*")); System.out.println("ABC321/OTHER888/".matches("[A-Za-z0-9]+/?[A-Za-z0-9]*"));
Output:
true true true false false
source share