using regex to get part of a string ?

Hi there, i wonder, is it possible to use regular expressions to partially select a string?

I have a bunch of server names which look like this

server1z-test
server2z2
server45z-primary
server13z3

I want to extract up to and including the 'z' in the server name, so for example

server1z
server2z
server45z
server13

everything to the right of the 'z' im not interested in

using the example above, is this possible using regular expressions?

Something like this?

sed 's/\(.*z\).*/\1/'

Or shorter:

sed 's/z.*/z/'

With shell variable substring:

echo ${var/z*/z}

That is non standard. The portable syntax is:

echo "${var%"${var#*z}"}"

Ok, thanks for the filling up!

thanks guys, If you get a chance, would you be able to explain to me what exactly is happening with these two commands

echo "${var%"${var#*z}"}"

sed 's/\(.*z\).*/\1/'

the var% is somewhat confusing and with regards to the second snippet, why is the "1" at the end... Thank you for posting those, if theres any way youd be able to explain them that would be great