问题描述
我不知道的一般规则在bash使用常规的前pression在if语句?
I wonder the general rule to use regular expression in if clause in bash?
下面是一个例子。
$ gg=svm-grid-ch
$ if [[ $gg == *grid* ]] ; then echo $gg; fi
svm-grid-ch
$ if [[ $gg == ^....grid* ]] ; then echo $gg; fi
$ if [[ $gg == ....grid* ]] ; then echo $gg; fi
$ if [[ $gg == s...grid* ]] ; then echo $gg; fi
$
为什么最后三个不匹配?
Why the last three fails to match?
希望你可以给尽可能多的一般规则成为可能,不只是在这个例子。
Hope you could give as many general rules as possible, not just for this example.
推荐答案
在使用glob模式,一个问号重新presents单个字符,并重新$ P $星号psents零个或多个字符的序列:
When using a glob pattern, a question mark represents a single character and an asterisk represents a sequence of zero or more characters:
if [[ $gg == ????grid* ]] ; then echo $gg; fi
在使用常规的前pression,点重新presents单个字符,并重新presents零个或多个preceding字符的星号。因此,。*
重presents零个或多个任意字符, A *
重新presents零个或多个一, [0-9] *
重presents零个或多个数字。另一种有用的酮(其中许多)是重新presents一个或多个preceding字符的加号。因此, [A-Z] +
重presents一个或多个小写字母字符(在C语言环境 - 和其他一些)。
When using a regular expression, a dot represents a single character and an asterisk represents zero or more of the preceding character. So ".*
" represents zero or more of any character, "a*
" represents zero or more "a", "[0-9]*
" represents zero or more digits. Another useful one (among many) is the plus sign which represents one or more of the preceding character. So "[a-z]+
" represents one or more lowercase alpha character (in the C locale - and some others).
if [[ $gg =~ ^....grid.*$ ]] ; then echo $gg; fi
这篇关于使用常规的前pression在bash中,如果条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!