首先,您的正则表达式不太适用。例如,对于/abc/obj.txt 的情况,它无法匹配.txt 部分。见A demo of your regex。其次,在子表达式[a-zA-Z0-9\\.]中,不需要反斜杠字符; . 将被解释为没有它们的句点字符。第三,您应该在正则表达式的开头添加^,在正则表达式的末尾添加$,以确保您匹配所需的内容,并且输入中没有多余的内容。第四,您没有指定您使用的语言。
我在这里使用 Python:
import re
tests = [
'/abc/obj*',
'/abc/*',
'/*',
'/abc/obj1.txt'
]
# the regex: ^/([a-zA-Z0-9]+/)*(\*|([a-zA-Z0-9]+(\*|(\.[a-zA-Z0-9]+)?)))$
for test in tests:
m = re.match(r"""
^ # the start of the string
/ # a leading /
([a-zA-Z0-9]+/)* # 0 or more: abc/
(\* # first choice: *
| # or
([a-zA-Z0-9]+ # second choice: abc followed by either:
(\*|(\.[a-zA-Z0-9]+)?))) # * or .def or nothing
$ # the end of the string
""", test, flags=re.X)
print(test, f'match = {m is not None}')
打印:
/abc/obj* match = True
/abc/* match = True
/* match = True
/abc/obj1.txt match = True
Regex Demo
但是当我阅读https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html 的对象键规范时,您的测试用例似乎不是有效示例,因为那里显示的示例都没有前导/ 字符。看起来* 字符应该被视为任何其他字符,并且可以在任何位置多次出现。这使得正则表达式实际上更简单:
^[a-zA-Z0-9!_.*'()-]+(/[a-zA-Z0-9!_.*'()-]+)*$
Regex Demo
新代码:
import re
tests = [
'abc',
'-/abc/(def)/!x*yz.def.hij'
]
# the regex: ^[a-zA-Z0-9!_.*'()-]+(/[a-zA-Z0-9!_.*'()-]+)*$
for test in tests:
m = re.match(r"""
^ # the start of the string
[a-zA-Z0-9!_.*'()-]+ # 1 or more: ~abc*(def)
(
/
[a-zA-Z0-9!_.*'()-]+
)* # 0 or more of /~abc*(def)
$ # the end of the string
""", test, flags=re.X)
print(test, f'match = {m is not None}')
打印:
abc match = True
-/abc/(def)/!x*yz.def.hij match = True