当前位置:  开发笔记 > 编程语言 > 正文

在PHP中为正则表达式添加注释的正确方法

如何解决《在PHP中为正则表达式添加注释的正确方法》经验,为你挑选了1个好方法。

我正在尝试添加注释以使正则表达式更清晰

// Strip any URLs (such as embeds) taken from http://stackoverflow.com/questions/6427530/regular-expression-pattern-to-match-url-with-or-without-http-www
$pattern =
    '(                               # First capturing group
            (http|https)             # Second capturing grout,matches wither http or https
        \:\/\/)?                     # End of first capturing group, matches :// exactly
        [                            # Match any char in the following list. the + after the closing bracke means greedy
            a-z                      # Any char between a and z
            A-Z                      # Any char between A and Z
            0-9                      # Any char between 0 and 9
            \.\/\?\:@\-              # ./?:@- literally ( any one of them )
            _=#                      # _=# any of these thre chars
        ]+                           # end of list
        \.                           # matches .
        (                            # third caturing group
            [                        # start of list
                a-z                  # Any char between a and z
                A-Z                  # Any char between A and Z
                0-9                  # Any char between 0 and 9
                \.\/\?\:@\-          # ./?:@- literally ( any one of them )
                _=#                  # _=# any of these thre chars
            ]                        # end of list
        )*                           # end of capturing group with greedy modifier';
$excerpt = preg_replace("/$pattern/x", '', $excerpt );

但是我得到了警告

警告:preg_replace():第280行的未知修饰符'/'

我该如何评论呢?



1> segFault..:

这可能不是最干净的方法,但您可以将每个部分括在引号中并将它们连接起来.

这样的事情应该有效:

$pattern =
    '('.                             // First capturing group
        '(http|https)'.              // Second capturing grout,matches wither http or https
    '\:\/\/)?'.                      // End of first capturing group, matches :// exactly
    ...   

或者我在PHP文档中找到了这个.

所以我想这也会起作用,但你正在使用x修饰符,而且应该已经有效了.

如果设置了PCRE_EXTENDED选项,则字符类外部的未转义#字符会引入注释,该注释将继续到模式中的下一个换行符.

这表示字符集中的所有注释[...]都无效.

以下是与PCRE_EXTENDED修饰符一起使用的工作示例:

$pattern = '
    (                              # First capturing group
        (http[s]?)                 # Second capturing grout,matches wither http or https
    \:\/\/)?                       # End of first capturing group, matches :// exactly
    [a-zA-Z0-9\.\/\?\:@\-_=#]+     # [List Comment Here]
    \.                             # matches .
    (                              # third caturing group
        [a-zA-Z0-9\.\/\?\:@\-_=#]  # [List Comment Here]
    )*                             # end of capturing group with greedy modifier
';


最好将分隔符更改为"〜"
推荐阅读
小妖694_807
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有