skrtxao
November 28, 2011, 10:38am
1
Hi,
I'm trying to validate if a string matches a regular expression, but it is not working. Am I missing something? Do I need to scape any of the characters?
if echo 'en-GB' | egrep '([a-zA-Z]{1,8})(-[a-zA-Z0-9]{1,8})*' >/dev/null; then
echo Valid value
fi
Thanks in advance
CarloM
November 28, 2011, 10:41am
2
Your regexp works with GNU grep, but you might need to escape the interval expressions ( \{1,8\}
) in some versions.
skrtxao
November 28, 2011, 10:51am
3
Thanks CarloM.
I tried this:
if echo 'en-GB' | egrep '([a-zA-Z]\{1,8\})(-[a-zA-Z0-9]\{1,8\})*' >/dev/null; then
echo Valid value
fi
but still didn't work. Is anything else that might be wrong?
Your first post works for me...
root@bt:~# if echo 'en-GB' | egrep '([a-zA-Z]{1,8})(-[a-zA-Z0-9]{1,8})*' >/dev/null; then
> echo Valid value
> fi
Valid value
which is your OS?
--ahamed
skrtxao
November 28, 2011, 11:03am
5
Hi ahamed,
I believe is SunOS 5.10...
CarloM
November 28, 2011, 11:07am
6
Use /usr/xpg4/bin/egrep
- Solaris egrep doesn't support interval expressions at all.
1 Like