matching a regex using egrep not working

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

Your regexp works with GNU grep, but you might need to escape the interval expressions ( \{1,8\} ) in some versions.

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

Hi ahamed,

I believe is SunOS 5.10...

Use /usr/xpg4/bin/egrep - Solaris egrep doesn't support interval expressions at all.

1 Like

That seems to work!

Thanks for the help.

skrtxao.