dscjp19831212 2014-08-04 23:08
浏览 87

双引号中字符串的正则表达式不起作用?

I'm trying to get all strings wrapped in double quotes with this regexp:

"(?:[^"\\]|\\.)*"

I already tried it on this site: http://www.phpliveregex.com/ and it works, but when i put it in my php code like this:

if( preg_match('/"(?:[^"\\]|\\.)*"/', $input_line, $output_array) )
{
.
.
.
}

I'm getting this error:

Warning:  preg_match(): Compilation failed: missing terminating ] for character class at offset 15

what am i missing?

SOLVED:

AS mario pointed out, a backslash was being escaped by PHP I got it working like this:

if( preg_match('/"(?:[^"\\\]|\\.)*"/', $sLine, $matches) ){
.
.
.
}
  • 写回答

1条回答 默认 最新

  • doulvli9462 2014-08-04 23:13
    关注

    The most reliable way to define regexes in PHP is like so:

    $regex = <<<'REGEX'
    /"(?:[^"\\]|\\.)*"/
    REGEX;
    

    Admittedly, it's not the most readable, but it ensures that the string is passed as-is to the regex engine and not interpreted by PHP in any way.

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料