我正在学习Linux命令行,并遇到了一个使我感到困惑的正则表达式。
有人能不能把这事做一遍,解释一下它在做什么?我知道这第一部分,因为读将使用答复作为一个变量,如果没有提供。
read -p "Enter a single item > "
# is input a valid filename?
if [[ $REPLY =~ ^[-[:alnum:]\._]+$ ]]; then
echo "'$REPLY' is a valid file name."比如为什么.-在里面?是否检查$REPLY的开头是否为破折号?任何帮助都将不胜感激。
发布于 2014-03-27 16:46:40
表达方式是:
^ -- Start of the string
[ -- Character class start
- -- A literal dash
[:alnum:] -- POSIX-style alphanumeric (a-z, 0-9)
. -- A literal period
_ -- A literal underscore
] -- End character class
+ -- One or more of anything in the character class
$ -- End of the string-是一个文字破折号。它位于字符类的开头,因此它不会被解释为类范围。例如,[a-c]是a, b, c,而[-ac]是-, a, c
另外,这是对文件名的不完整检查。据我所知,文件名可以包含除目录分隔符(例如/)以外的任何字符--偶数空格和换行符。
https://stackoverflow.com/questions/22693975
复制相似问题