我正在使用这个正则表达式
/foo/bar/([^ ]*)/{0,1}(?!.)
基本上应该允许什么
/foor/bar/xxxxxx/
|__optional but nothing after this
字符串可能以字符“/”结尾,但在此之后不允许任何内容。 为了实现这一点,我做了
/foo/bar/([^ ]*)/{0,1}(?!.)
A B C
A is ([^ ]*) means allow anything except a space
B is /{0,1} which basically means that the character / should be optional
C is (?!.) which basically is a negative lookahead that no character should be next
我这样做是对的吗?如果我使用字符串
/foo/bar/somestuff/Whatever
如果符合正则表达式的资格。我希望它因为Whatever而无法获得资格。我做错了什么
答案 0 :(得分:2)
您需要在(A)中排除/
:
/foo/bar/([^ /]*)/{0,1}(?!.)
您还可以使用?
代替{0,1}
并匹配输入结尾($
)而非负面预测来进一步简化此操作:
/foo/bar/([^ /]*)/?$