If you don't mind if your path has mixed delimiters like aaa-bbb/ccc
then you can get it done in one go reasonably easily with:
"^(?:[A-z0-9]+[-_\/]?)*$"
This will accept aaa-bbb/ccc
, and aaa/
but reject aaa--bbb
. If you want to ensure that you have only one type of delimiter, then we can create one repeated block for each delimiter type:
"^(?:(?:[A-z0-9]+-?)*|(?:[A-z0-9]+_?)*|(?:[A-z0-9]+\/?)*)$"
This will still tolerate trailing delimiters, which you can get rid of that by adding another group to the end:
"^(?:(?:[A-z0-9]+-?)*|(?:[A-z0-9]+_?)*|(?:[A-z0-9]+\/?)*)[A-z0-9]+$"
If you want allow leading delimiters, just move them before the group. Also notice the extra \s*
to allow trailing whitespace.
"^(?:(?:-?[A-z0-9]+)*|(?:_?[A-z0-9]+)*|(?:\/?[A-z0-9]+?)*)\s*$"
Not exactly easy on the eyes, but it does work (in perl at least).