我想使用preg_replace将“* @license until */
”替换为“testing
”。
我该怎么做?
我的文字如下:
/*
* @copyright
* @license
*
*/
我希望每个人都能正确理解我的问题。
答案 0 :(得分:2)
这是一个完成你想要的正则表达式(在多行模式下运行)
^\s*\*\s*@license(?:(?!\s*\*/)[\s\S])+
它匹配被击中的部分:
/* * @copyright* @license **/
说明:
^ ~ start-of-string \s* ~ any number of white space \* ~ a literal star \s* ~ any number of white space @license ~ the string "@license" (?: ~ non-capturing group (?! ~ negative look ahead (a position not followed by...): \s* ~ any number of white space \* ~ a literal star / ~ a slash ) ~ end lookahead (this makes it stop before the end-of-comment) [\s\S] ~ match any single character )+ ~ end group, repeat as often as possible
请注意,根据preg_replace()
规则,仍必须根据PHP字符串规则和对正则表达式进行转义。
编辑:如果您愿意 - 让完全确定在匹配的文本后面确实存在评论结束标记,则可以像这样展开正则表达式:
^\s*\*\s*@license(?:(?!\s*\*/)[\s\S])+(?=\s*\*/) ↑ positve look ahead for +-----------an end-of-comment marker
答案 1 :(得分:0)
嗯,这不是太难。您需要做的就是使用s
修饰符(PCRE_DOT_ALL,它使正则表达式中的.
匹配新行):
$regex = '#\\*\\s*@license.*?\\*/'#s';
$string = preg_replace($regex, '*/', $string);
那对你有用(注意,未经测试)......