Php preg_match代码用于分割以数字结尾的字符串

considers a string that is based on the following : 1. can contain any alphanumeric characters 2. the string ends with number(s)

Now by using preg_match($expression, $subject, $parts) I want to split the string in such a manner that the ending numbers and the rest of the string is stored in the $parts array separately.

for example: kim2bn88 should result in two parts: kim2bn and 88

But I am not able to figure out the regex. Kindly help!

I think you want a regex like as follows:

/^(\w+)(\d+)$/

Assuming you always have non-digits before the final digits.

This gives you two capture groups. The first are word characters (alphanumeric + _) at the start of the string. The second are digits at the end of the string.

The regexp your are searching for must use the end boundary “$”.

/(\d+)$/

If you want to avoid a split, next to this search, try this double “group and extract”:

/((\d)+)$/

$parts will contain "88", "8", "8". The first item of the array will be the whole numerical subset, then one item per digit.