Hi,
i am java guy and new to unix.
I want to validate date pattern using Regex expression
here is the sample program i have written.
#!/bin/sh
checkDate="2010-04-09"
regex="\\d{4}-\\d{2}-\\d{2}\$"
echo $regex
if [[ $checkDate =~ $regex ]]
then
echo "OK"
else
echo "not OK"
fi
But the ouput is "not Ok"
Please help me.
You could try this with standard globbing:
#!/bin/bash or #!/bin/ksh93
checkDate="2010-04-09"
regex="[1-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]"
echo $regex
if [[ $checkDate =~ $regex]]
then
echo "OK"
else
echo "not OK"
fi
Change your regex to
regex='[0-9]{4}-[0-9]{2}-[0-9]{2}$'
\d is part of the extended regex set, which bash does not support by default.
There may be a way to get it to use the extended set, but I've haven't checked.