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 :
Please note that with PHP, you need to use the preg_match method and surround the regexp with slashes.
```php
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