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 :
^\d{2}[\/\-]\d{2}[\/\-]\d{4}$
It seems to work, I tried it on several regex tests websites.
My shell code is :
REGEX_DATE="^\d{2}[\/\-]\d{2}[\/\-]\d{4}$"
echo "$1" | grep -q $REGEX_DATE
echo $?
The "echo $?" returns 1 no matter is the string I'm putting in parameter.
Do you guys have an idea ?
Thanks !
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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments