Try
^(?:[^/]+/)*[^/]+$
Or if [a-zA-Z0-9]+
is really necessary, try
^(?:[a-zA-Z0-9]+/)*[a-zA-Z0-9]+$
I personally think, first one should be faster though
S.Mark
2010-04-22 14:17:05
Try
^(?:[^/]+/)*[^/]+$
Or if [a-zA-Z0-9]+
is really necessary, try
^(?:[a-zA-Z0-9]+/)*[a-zA-Z0-9]+$
I personally think, first one should be faster though
A better one would be more deterministic, and without capturing groups:
^[a-zA-Z0-9]+(?:/[a-zA-Z0-9]+)*$
This way you don't have overlapping groups, and you're not capturing things unnecessarily.
I think the problem is that with the optional /
strings can be parsed in just too many ways, in particular any sequence of N alphamerics can be matched in N-1 ways. What about:
^([a-zA-Z0-9]+[/])*[a-zA-Z0-9]+$
i.e. 0 or more (1+ alphamerics-then-slash) and then one last helping of (1+ alphamerics). (Of course as other answers mention you can also put a ?:
right after the open paren if you want the group to be non-capturing).