I'm trying to validate entered URI path with wildcard pattern in a form of asterisks single(*) or double (**). For example: /path1/*/path2/
or /path1/path2/**/path3
I came up with regexp ^(?:\/\w+\/?|\/\*\/?|\/\*\*\/?)+$
that matches all valid path from the list below except: /foo*
and /foo**
and does not match invalid except one /foo//bar
Could you suggest a better regex to cover all cases and maybe more optimized than my.
https://regex101.com/r/U65utY/1
Considering valid path like:
and invalid path like:
You can use
^(?:/(?:\*{1,2}|\w+\*{0,2}))+/?$
See the regex demo
Details
^
- start of string(?:/(?:\*{1,2}|\w+\*{0,2}))+
- one or more occurrences of
/
- a /
char(?:\*{1,2}|\w+\*{0,2})
- one of the alternatives:
\*{1,2}|
- one or two asterisks, or\w+\*{0,2}
- one or more word chars and then zero, one or two asterisks/?
- an optional /
$
- end of string.