如何使用preg_match获得字符串的结束


How to get the end of string with preg_match?

我得到了这个字符串:

from:100000238267321=to:100000238267322=somethingelse: hey james, heard the news, mike is returning tomorrow!

这个preg_match命令:

#(?P<from>(?:'w|'s)+):(?P<idfrom>'d+)=(?P<to>(?:'w|'s)+):(?P<toid>'d+)=(?P<somethingelse>(?:'w|'s)+):(?P<somethingelsetxt>(?:'w|'s)+)#

我需要得到'somethingelse:'部分之后的所有文本。对于上面的preg_match,它只会得到'hey james'后面的逗号。返回:

[0] from:100000238267321=to:100000238267322=somethingelse:hey james
[from]  from
[1] from
[idfrom]    100000238267321
[2] 100000238267321
[to]    to
[3] to
[toid]  100000238267322
[4] 100000238267322
[somethingelse] somethingelse
[5] somethingelse
[somethingelsetxt]  hey james
[6] hey james

我该怎么办?

使用.*$获取从当前点到字符串末尾的所有内容。确保设置了multiline修饰符,以便.匹配换行符,除非您确定不会有任何换行符。

您可以使用explosion来获取最后一个数组值:

$string = 'from:100000238267321=to:100000238267322=somethingelse: hey james, heard the news, mike is returning tomorrow!';
$value = explode(':',$string);
echo $value[3];

看到codepad。

更新

要解决正则表达式问题,请在somethingelsetxt正则表达式中添加"| ",如下所示:

(?P<somethingelsetxt>(?:'w|'s|,)

也会得到逗号。如果你愿意,你也可以包括一个"|!"