php - 正则表达式查找不在引号中的内容

标签 php regex

我正在尝试查找引号外的参数(单或双)

$sql = "
INSERT INTO Notifify (to_email, msg, date_log, from_email, ip_from)
VALUES
    (
        :to_email,
        'test teste nonono',
        '2013-02-01 10:48:27',
        'bar@foo',
        :ip_from
    )
 ";

  $matches = array();
  preg_match_all('/:[A-Za-z0-9_]*/', $sql, $matches);

上面的代码会产生如下结果,

print_r($matches);//数组(:to_email, :48, :27, :ip_from)

我只想要:

:to_email
:ip_from

最佳答案

'/^\\s*:[A-Za-z0-9_]*/m'

应该这样做,检查行首和空格,并确保将 RegEx 查询设置为多行。

编辑

preg_match_all('/(?:^\\s*)(:[A-Za-z0-9_]+)/m', $sql, $matches);
print_r($matches[1]);

这使用被动非捕获组 (?:) 将正确的结果放入索引 1 处的 matches 变量的子数组中,没有空格填充。

关于php - 正则表达式查找不在引号中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14647560/

相关文章:

regex - 查找重复字符的单词

javascript - 之间的区别? :, ?!和?=

php - 如何在 AJAX 请求后使用数据库信息填充 View

php - 如何使用 CentOS 7 在 PHP 7.2 上安装 zipArchive?

php - Composer - 请求包 [0.0.9] 作为 [0.0.5,0.0.6] 存在,但这些被您的约束拒绝

Python re.findall() 和优先级

c++ <regex> 搜索不匹配

php - Coinbase Pro 费用和总额

php - 如何在 CodeIgniter base_url 中显示我的图像

javascript - RegExp.$1 是做什么的