爱国者
爱国者

Reputation: 4348

What is wrong with this BASH regular expression

$ reg='(\.js)|(\.txt)|(\.html)$'
$ [[ 'flight_query.jsp' =~ $reg ]]
$ echo $?
0

*.jsp should not be matched based on the regular expression, but actually doesn't.

Any suggestions?

Upvotes: 2

Views: 145

Answers (1)

creemama
creemama

Reputation: 6665

A useful comment was deleted. The comment suggested that operator precedence was the reason why the regular expression was passing. He suggested the following regular expression as a fix.

$ reg='(\.js|\.txt|\.html)$'
$ if [[ 'flight_query.jsp' =~ $reg ]]; then echo 'matches'; else echo "doesn't match"; fi
doesn't match
$ if [[ 'flight_query.js' =~ $reg ]]; then echo 'matches'; else echo "doesn't match"; fi
matches

This regular expression works as well (\.js$)|(\.txt$)|(\.html$).

Upvotes: 8

Related Questions