extraction from a path

Hi,
Can you help me on this two problems?

how can i get :

from input: /ect/exp/hom/bin ==> output: exp
and
from input: aex1234 =====>output: ex

thanks,

Please read the rules (4): no duplicate or cross-posting!
Continue here.