您如何注释掉PHP中正则表达式的*/部分 [英] How Do You Comment Out the */ Part of a Regular Expression in PHP

查看:52
本文介绍了您如何注释掉PHP中正则表达式的*/部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有preg_replace函数,我正在调用该函数并放在多行上以提高可读性,但是正则表达式中的 */字符使注释混乱.如何注释掉所有这些行而不将它们全部移到一行?

I have preg_replace function that I'm calling and putting on multiple lines for readability but the */ characters in the regex mess up the comments. How can I comment out all these lines without moving them all onto one line?

return preg_replace('/.*/',
    'Lorem Ipsum' .
    'More Lorem Ipsum'
    ,
    $foo);

推荐答案

您可以使用其他正则表达式模式分隔符:

You could use a different regex pattern delimiter character:

return preg_replace('#.*#',
    'Lorem Ipsum' .
    'More Lorem Ipsum'
    ,
    $foo);

分隔符是PCRE(与Perl兼容的常规表达)的功能.无需使用其他定界符就可以进行PHP配置.

The delimiter character is a feature of PCRE (Perl Compatible Regular Expresssion). No PHP configuration is needed to use a different delimiter.

Regexp引用类似运算符

...您可以使用任意一对非字母数字,非空格字符作为分隔符.这对于匹配路径名特别有用包含"/"的符号,以避免LTS(倾斜的牙签综合征).

...you can use any pair of non-alphanumeric, non-whitespace characters as delimiters. This is particularly useful for matching path names that contain "/", to avoid LTS (leaning toothpick syndrome).

类似于Quote和Quote的运算符

不带括号的定界符前后使用相同的字符,但是四种ASCII括号(圆形,尖角,正方形,卷曲)都嵌套

Non-bracketing delimiters use the same character fore and aft, but the four sorts of ASCII brackets (round, angle, square, curly) all nest

这些都是有效的:

'/.*/'
'#.*#'
'{.*}' /* Note that '{.*{' would be incorrect. */

看看有关PCRE模式的PHP文档,很好的概述.

这篇关于您如何注释掉PHP中正则表达式的*/部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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