preg_replace 特定模式

发布于 2024-09-13 21:29:41 字数 203 浏览 4 评论 0原文

我想使用 preg_replace 将“* @license until */”替换为“testing”。
我该怎么做呢?
我的文字如下:

/*
 * @copyright  
 * @license  
 *
 */

我希望大家能够正确理解我的问题。

I would like to use preg_replace to replace "* @license until */" with "testing".
How can I do it?
My text is the one below:

/*
 * @copyright  
 * @license  
 *
 */

I hope everyone have understand my question correctly.

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

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

发布评论

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

评论(2

掐死时间 2024-09-20 21:29:41

这是一个可以执行您想要的操作的正则表达式(以多行模式运行)

^\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

请注意,正则表达式仍然必须根据 PHP 字符串规则进行转义到 preg_replace() 规则。

编辑:如果您愿意 - 要绝对确定在匹配的文本后面确实有一个注释结束标记,正则表达式可以像这样扩展:

^\s*\*\s*@license(?:(?!\s*\*/)[\s\S])+(?=\s*\*/)
                                      ↑           positve look ahead for 
                                      +-----------an end-of-comment marker

Here is one regex that does what you want (run it in multi-line mode)

^\s*\*\s*@license(?:(?!\s*\*/)[\s\S])+

It matches the part that is striked:

/*
 * @copyright  
 * @license  
 *
 */

Explanation:

^              ~ 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

Note that the regex must still be escaped according to PHP string rules and according to preg_replace() rules.

EDIT: If you feel like it - to make absolutely sure that there really is an end-of-comment marker following the matched text, the regex can be expanded like this:

^\s*\*\s*@license(?:(?!\s*\*/)[\s\S])+(?=\s*\*/)
                                      ↑           positve look ahead for 
                                      +-----------an end-of-comment marker
赴月观长安 2024-09-20 21:29:41

嗯,这并不太难。您需要做的就是使用 s 修饰符(PCRE_DOT_ALL,它使正则表达式中的 . 匹配新行):

$regex = '#\\*\\s*@license.*?\\*/'#s';
$string = preg_replace($regex, '*/', $string);

这应该适合您(注意,未经测试)。 ..

Well, it's not too hard. All you need to do is use the s modifier (PCRE_DOT_ALL, which makes a . in the regex match new lines):

$regex = '#\\*\\s*@license.*?\\*/'#s';
$string = preg_replace($regex, '*/', $string);

That should work for you (note, untested)...

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