如何在php中使用正则表达式来获取字符串末尾的字符[关闭]

I would like to parse a string and get characters from the end back to the first slash in the string. The string may change.

An example may be:

'string4\string3\string2\string1'

And I would want string1 from this example.

Another example string is:

'string3\string2\string1' 

And I would like to get string1 from this example. Can this be done in php using regular expressions?

Just break the string apart by the \ character and get the last part:

$parts = explode('\\', $string);
echo $parts[count($parts) - 1];

Demo