php 函数 preg_replace 正则表达式不起作用,语法问题

发布于 2024-10-08 01:53:29 字数 815 浏览 0 评论 0原文

我试图在受控脚本情况下使用 preg-replace 删除不必要的注释,但我的正则表达式不正确。有人知道我的正则表达式有什么问题吗? (我有 Apache/2.0.54 和 PHP/5.2.9

之前:

// Bla Bli Blue Blow Bell Billy Bow Bye
script var etc ();    // cangaroo cognac codified cilly celine cocktail couplet
script http://blaa.org    // you get the idea!

之后:

script var etc ();
script http://blaa.org

问题:要使用什么正则表达式?

# when comment starts on a new line, delete this entire line
# find [a new line] [//] [space or no space] [comment]
$buffer = preg_replace('??', '??', $buffer);

# when comment is halfway in script (    //  comment)
# find [not beginning of a line] [1 TAB] [//] [1 space again] [comment]
$buffer = preg_replace('??', '??', $buffer);

任何和所有建议都将被我重视+1,因为我非常接近解决这个谜语!

im trying to remove unnessary comments with preg-replace in controlled script situations, but my regex is incorrect. Anyone any ideas whats wrong with my regex? (i have Apache/2.0.54 & PHP/5.2.9

BEFORE:

// Bla Bli Blue Blow Bell Billy Bow Bye
script var etc ();    // cangaroo cognac codified cilly celine cocktail couplet
script http://blaa.org    // you get the idea!

AFTER:

script var etc ();
script http://blaa.org

PROBLEM: what regex to use?

# when comment starts on a new line, delete this entire line
# find [a new line] [//] [space or no space] [comment]
$buffer = preg_replace('??', '??', $buffer);

# when comment is halfway in script (    //  comment)
# find [not beginning of a line] [1 TAB] [//] [1 space again] [comment]
$buffer = preg_replace('??', '??', $buffer);

Any and All suggestions will be valued +1 by me, cuase im so darn close to solve this riddle!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摇划花蜜的午后 2024-10-15 01:53:29

尝试这个正则表达式:

/(?<!http:)\/\/[^\r\n]*/

不过要小心,考虑像这样的字符串:

<!-- 
// not a comment -->

or

/* 
// not a comment */

and

var s = "also // not // a // comment";

你可能想解决 https://...ftp://... ETC。

Try this regex:

/(?<!http:)\/\/[^\r\n]*/

Be cautious though, consider strings like:

<!-- 
// not a comment -->

or

/* 
// not a comment */

and

var s = "also // not // a // comment";

And you might want to work around https://... and ftp://... etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文