How do I use a regex in a shell script?

前端 未结 3 2030
暗喜
暗喜 2020-12-01 10:43

I am trying to match a string with a regex in a shell script. This string is a parameter of the script ( $1 ) and it is a date (MM/DD/YYYY) The regex I\'m trying to use is :

3条回答
  •  执念已碎
    2020-12-01 11:15

    I think this is what you want:

    REGEX_DATE='^\d{2}[/-]\d{2}[/-]\d{4}$'
    
    echo "$1" | grep -P -q $REGEX_DATE
    echo $?
    

    I've used the -P switch to get perl regex.

提交回复
热议问题