正则表达式查找不在引号中的内容 [英] Regex to find content not in quotes

查看:67
本文介绍了正则表达式查找不在引号中的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to find parameters outside quotes (Single ou Double)

$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);

以上代码会产生如下结果,

The above code will produce the follow result,

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

而我只想要:

:to_email
:ip_from

推荐答案

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

应该可以解决问题,检查行首和空格,并确保 RegEx 查询设置为多行.

Should do the trick, checking for beginning of line and white-space and make sure the RegEx query is set to multiline.

编辑

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

这使用了被动非捕获组 (?:),它可以放置正确的结果,而不会将空格填充到索引 1 处的匹配变量的子数组中.

This uses the passive non-capture group (?:) which puts the proper results, without the space padding into a sub array of the matches variable at index 1.

这篇关于正则表达式查找不在引号中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆