With Regex Spliting the string into Alphanumeric and Numeric part

Hi there

With shell script I'm trying to split the string into two parts. One is alphanumeric part, the other one is a numeric part.

dummy_postcode_1 = 'SL1' 
--> res_alpha = 'SL' and  res_numeric = '1' 
dummy_postcode_2 = 'S053' 
--> res_alpha = 'S' and  res_numeric = '053' 
dummy_postcode_3 = 'SO5R3' 
--> res_alpha = 'SO5R' and res_numeric = '3' 
dummy_postcode_4 = 'SO53R' 
--> res_alpha = 'SO53R' and res_numeric = ''
dummy_postcode_5 = '783652' 
--> res_alpha = '' and res_numeric = '783652'  

The logic of splitting these postcode strings is the rightmost alphanum character it seems and I could do that with the character checking but it seems to me this type of coding is a bit silly to do so.

Could you advise me how i can split them into two parts by using regex?

Thanks in advance,

Ozgur

With ksh and bash (with extglog option enabled) you can do :

echo "SL1
S053
SO5R3
SO5R
783652" |
while read var
do
   res_alpha=${var%%*([0-9])}
   res_num=${var#$tes_alpha}
   echo "$var = '$res_alpha' + '$res_num'"
done

Output:

SL1 = 'SL' + '1'
S053 = 'S' + '053'
SO5R3 = 'SO5R' + '3'
SO5R = 'SO5R' + ''
783652 = '' + '783652'

With all shells you can also do the work with expr :

echo "SL1
S053
SO5R3
SO5R
783652" |
while read var
do
   res_alpha=`expr "$var" : '\(.*[^0-9]\+\)[0-9]*'`
   res_num=`expr "$var" : "${res_alpha}\(.*\)"`
   echo "$var = '$res_alpha' + '$res_num'"
done

Output:

SL1 = 'SL' + '1'
S053 = 'S' + '053'
SO5R3 = 'SO5R' + '3'
SO5R = 'SO5R' + ''
783652 = '' + '783652'