【问题标题】:PHP preg_match Wildcard pattern searchPHP preg_match 通配符模式搜索
【发布时间】:2015-06-30 15:49:18
【问题描述】:

这是我之前的 SO 问题的扩展:PHP Get string after a specific regex pattern match

目前我正在使用这种模式:

$pattern = '/(?<=The following users in your Google Apps domain appear to be affected: )\S+/i';

我希望能够使用这样的东西:

$pattern = '/(?<=The following * affected: )\S+/i';

使用以下方法获得相同的结果:

if (preg_match($pattern, $eBody, $matches)) {
    echo $matches[0] . "# <br>";
}

【问题讨论】:

  • 我想你在following之后想要.*,而不仅仅是*

标签: php regex string preg-match wildcard


【解决方案1】:

您可以使用\K 转义序列来操作可变长度的后视:

$pattern = '/The following.*?affected: \K\S+/i';

【讨论】:

  • 谢谢你和阿努巴瓦!他早了几秒钟,但你的回复显示在最前面,而且更“完整”了一点。
【解决方案2】:

您可以使用\K(匹配重置):

/The following .*? affected: \K\S+/i

RegEx Demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    相关资源
    最近更新 更多