One minute
Extract characters after the last dash of a String RegExp
š“ Careful You’re reading an old article ! Some links might be broken and content may be outdated
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/
Read other posts