views:

87

answers:

3
+2  A: 

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
That worked, apart from allowing non-alpha-numeric characters. Thanks though.
Kieron
Yeah @Kieron, Thats allow any except /, but I think `[^/]+` should be faster than `[a-zA-Z0-9]+`, and also added a version with `[a-zA-Z0-9]+`.
S.Mark
+8  A: 

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.

Welbog
Perfect, thanks.
Kieron
+3  A: 

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).

Alex Martelli