gpt4 book ai didi

java - groovy 或 java : how to retrieve a block of comments using regex from/** ***/?

转载 作者:行者123 更新时间:2023-12-01 19:21:50 25 4
gpt4 key购买 nike

这对于 Java 专家来说可能是小菜一碟。请帮帮我:

我的程序中有一段注释,如下所示:

/********* 
block of comments - line 1
line 2
.....
***/

如何使用正则表达式检索“评论 block ”?

谢谢。

最佳答案

类似这样的事情应该做:

    String str =
"some text\n"+
"/*********\n" +
"block of comments - line 1\n" +
"line 2\n"+
"....\n" +
"***/\n" +
"some more text";

Pattern p = Pattern.compile("/\\*+(.*?)\\*+/", Pattern.DOTALL);
Matcher m = p.matcher(str);

if (m.find())
System.out.println(m.group(1));

(DOTALL 表示模式中的 . 也应该匹配换行符)打印:

block of comments - line 1
line 2
....

关于java - groovy 或 java : how to retrieve a block of comments using regex from/** ***/?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3885034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com