大佬教程收集整理的这篇文章主要介绍了PHP Regex 忽略注释行,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个来自 github 的 REGEX,它似乎工作正常,但它无法处理注释行。
$regex = '/(?:@import)(?:\\s)?(?:(?:(?:\\()(["\'])?(?:[^"\')]+)\\1(?:\\))|(["\'])(?:.+)\\2)(?:[A-Z\\s])*)+(?:;)/';
此 REGEX 将在如下内容上进行测试:
$content =
'// Layout & components // this should fail
@import "variables","mixins"; // this should valIDate
// @import "tools","functions"; // this should fail
// @import "media/vendor/bootstrap/sCSS/root"; // this should fail
@import "media/vendor/bootstrap/sCSS/reboot"; // this should valIDate
@import "../../../media/vendor/bootstrap/sCSS/tools"; // this should valIDate
@import url("some.CSS"); // this should fail
// @import url("some.CSS"); // this should fail
//@import url("some.CSS"); // this should fail
// @import some; // this should fail
//@import some; // this should fail
//@import"some"; // this should fail
//@importsome; // this should fail
在上面的例子中,第一行将被忽略,因为它没有 '@import' 但第二行仍然通过(不会被忽略),因为它不是一个处理案例。
我设置了一个无法实时运行的 regex101,但它可以在我的本地环境中运行。
我需要什么:
(^\\n?!:(\/\/))
之类的东西,但我不知道这是否会起作用,或者它不会做其他事情。@import url("bar.CSS");
之类的情况,我想删除此情况,使其仅适用于 `@import "bar.sCSS"。preg_replace("/^\\/{2}.*|\\/\\*.*\\*\\/$/",'',$content)
但它破坏了我的代码,所以我想知道你认为什么是最好的。感谢任何回复。谢谢。
我建议使用
$regex = "/^\\s*@import\\s+(\"|').*\\1\\s*[^;]*;.*$/m";
(您可以在这里尝试:https://regex101.com/r/3gou5o/2)
这仅匹配导入规则,在行开始前没有空格,导入 url 后只有分号。请注意,这也匹配无效的导入。
示例:
// Layout & components -> does not match
// @import "media/vendor/bootstrap/scss/root"; -> does not match
@import url("media/vendor/bootstrap/scss/reboot"); -> does not match
@import url("media/vendor/bootstrap/scss/type"); -> does not match
@import "media/vendor/bootstrap/scss/images" screen; -> matches
@import "media/vendor/bootstrap/scss/containers"; -> matches
@import 'media/vendor/bootstrap/scss/components' ; -> matches
@import "images","screen"; -> matches
@import "this/is"/invalid"; -> matches
以上是大佬教程为你收集整理的PHP Regex 忽略注释行全部内容,希望文章能够帮你解决PHP Regex 忽略注释行所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。