easy_weezy
easy_weezy

Reputation: 37

Regex matching (anything)(dash)(numbers) and then replace

I need a little help on a regex pattern.

I have some strings ending with a dash and a number to the end to the string.

Example:

asddasasdasddasadsdsa-1
asdasdasdasd-11
asasdasdasdasd-234

I want to remove the (dash)(number to the end of string) so I need to match them and replace them with "".

I want to remove the LAST dash that is followed by only numbers to the end of the string.

Examples:

asdasd-1-1 must be asdasd-1 (last dash and number removed)

asdasd-1-1a must not change (there is no "(dash)(numbers only to the end of string)" so nothing changes)

Thank you!

Upvotes: 1

Views: 1071

Answers (1)

Toto
Toto

Reputation: 91488

Try:

$str = preg_replace('/-\d+$/', '', $str);

Upvotes: 7

Related Questions