Extract characters after the last dash of a String – RegExp

I wanted to extract characters after the last dash of a string. Regular expression are such a powerful tool to use in these cases. Here’s the one I needed for my problem :

(\w+)[^-]*$

Please note that with PHP, you need to use the preg_match method and surround the regexp with slashes.

preg_match ('/(\w+)[^-]*$/', 'cgo3020-red-xs', $results); // returns xs in $results[0]

Here’s a pretty convenient tool to test your regexp:  https://regex101.com/