Pattern Matching

Hello All,

I would like to have your suggestions to find out whether some characters are present in the alphanumeric string.

For example, i have string like 9235793579usa9485 and i want to know how to use KSH pattern matching whether it has any alphabets ( either a-z or A-Z ) in it.
Thanks in advance!

One way:

var=9235793579usa9485
if [[ "$var" = *[a-zA-Z]* ]]
then
   print "String contains alphabetic characters"
else
   print "No alphabetic character."
fi

Jean-Pierre.

1 Like

You could use the case compound command:

$ LANG=C
$ s=9235793579usa9485                                                                                                       
$ case $s in (*[A-z]*) echo ok;; esac                                                                                    
ok
$

If your shell supports POSIX character classes, you could use [:alpha:].

Consider the following:

$ s=9235793579�
$ case $s in (*[A-z]*) echo ok;; esac
$ zsh -c 's=9235793579�; case $s in (*[A-z]*) echo ok;; esac'
$ zsh -c 's=9235793579�; case $s in (*[[:alpha:]]*) echo ok;; esac'
ok
1 Like

it worked. Thanks aigles and radoulov for your quick responses.