Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有点困惑:为什么使用命令行中的 grep 正则表达式可以很好地工作,并且我在 bash 条件语句中使用完全相同的正则表达式,它根本不起作用?
我想匹配所有只包含字母的字符串,因此我的正则表达式是: ^[a-zA-Z]\+$。
^[a-zA-Z]\+$
请你帮忙解决这个问题?
这是我的 bash 代码的片段
if ! [[ "$1" =~ '^[a-zA-z]+$' ]] ; then echo "Error: illegal input string." >&2 exit 1 fi
不要逃避+.
+
这对我有用:
$ [[ "Abc" =~ ^[a-zA-Z]+$ ]] && echo "it matches" $ it matches
此外,您不需要在正则表达式周围加上单引号。以下对我有用:
if ! [[ "$1" =~ ^[a-zA-z]+$ ]] ; then echo "Error: illegal input string." >&2 exit 1 fi