dtczp02204 2012-03-03 23:47
浏览 52

为什么PHP preg_ *函数需要regexp分隔符? [关闭]

This is bugging me:

Why preg_match('/pattern/', $haystack) instead of preg_match('pattern', $haystack)? Everything I've seen just states as a fact that they're necessary, and mentions that you can use alternate delimiters.

But, it's a function that defines its own interface outside of the string. It has a flags argument. Adding intra-string syntax seems capricious.

Is it something inherited from pcre that the authors were just not interested in working around? Yet another perverse fact of PHP? Or is there a justification?

  • 写回答

4条回答 默认 最新

  • dongshou1991 2012-03-03 23:51
    关注

    The delimiters are for compatibility with Perl. Perl regular expressions use the delimiters, and rely on the end delimiter to signify the start of modifier flags, like i for case insensitivity, for example.

    // Match alpha-numeric, case insensitive, multiline
    preg_match('/^[a-z0-9]+$/im', $input);
    

    The optional flags argument to preg_match() does not implement the regular expression flags like i that follow the second delimiter. They serve a different function, and indeed PREG_OFFSET_CAPTURE is the only flag available there. This is not to say the regex flags could not have been implemented as another function parameter. They certainly could have, but Perl-compatibility is the goal here.

    PHP is not the only language that borrows directly from Perl to implement regular expressions. JavaScript does to some degree, and Ruby even implements Perl's =~ operator for regular expression matches.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP