Moi*_*man 11
如果您需要查找字符串中的最后一个单词,请执行以下操作:
m/
(\w+) (?# Match a word, store its value into pattern memory)
[.!?]? (?# Some strings might hold a sentence. If so, this)
(?# component will match zero or one punctuation)
(?# characters)
\s* (?# Match trailing whitespace using the * because there)
(?# might not be any)
$ (?# Anchor the match to the end of the string)
/x;
Run Code Online (Sandbox Code Playgroud)
在此语句之后,$ 1将保留字符串中的最后一个单词.您可能需要通过添加更多标点符号来扩展字符类[.!?].
在PHP中:
<?php
$str = 'MiloCold is Neat';
$str_Pattern = '/[^ ]*$/';
preg_match($str_Pattern, $str, $results);
// Prints "Neat", but you can just assign it to a variable.
print $results[0];
?>
Run Code Online (Sandbox Code Playgroud)