简体   繁体   中英

Are = and =~ equivalent to check simple regular expressions?

Reading a comment by Charles Duffy I ran across a question: is there any difference in using = or =~ to check simple regular expressions?

When we just want to check if a string is part of another string, these options are equivalent:

$ [[ hello = *el* ]] && echo "yes"
yes
$ [[ hello =~ el ]] && echo "yes"
yes

Same happens if the store the regular expression in a variable:

$ re="[0-9]"
$ number=2
$ [[ $number = $re ]] && echo "yes"
yes
$ [[ $number = *$re* ]] && echo "yes"
yes

However, checking non-literal strings does not work with = and needs the usage of =~ :

$ date=20160620
$ [[ $date = [0-9]{4}(0[0-9]|1[0-2])([0-2][0-9]|3[0-1]) ]] && echo "yes"
bash: syntax error in conditional expression: unexpected token `('
bash: syntax error near `[0-9]{4}(0'
$ [[ $date =~ [0-9]{4}(0[0-9]|1[0-2])([0-2][0-9]|3[0-1]) ]] && echo "yes"
yes
$ [[ $date =~ ^[0-9]{4}(0[0-9]|1[0-2])([0-2][0-9]|3[0-1])$ ]] && echo "yes"
yes

What should be used in general performance and POSIX-wise?

Note all of these occurs is based on my GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu) .

Only glob pattern matching is supported using = and == in BASH.

Your first 2 examples are producing the same result because you are matching *el* (glob) in first and el (will be same in glob or regex or plain string) in 2nd example.

But when you use proper regex using character classes, quantifiers, anchors etc then you must use =~ only to match a regular expression.

That is precisely the reason of syntax error in 2nd block because shell is trying to interpret given regex as a glob pattern.


From Bash Reference Manual → 3.2.4.2 Conditional Constructs :

 [[ expression ]] 

When the '==' and '!=' operators are used, the string to the right of the operator is considered a pattern and matched according to the rules described below in Pattern Matching , as if the extglob shell option were enabled. The '=' operator is identical to '=='.

An additional binary operator, '=~', is available, with the same precedence as '==' and '!='. When it is used, the string to the right of the operator is considered an extended regular expression and matched accordingly (as in regex3)).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM