gpt4 book ai didi

用于匹配多行嵌套注释的java正则表达式

转载 作者:行者123 更新时间:2023-12-01 11:06:04 26 4
gpt4 key购买 nike

我正在尝试编写一个java正则表达式来从我的代码中删除所有注释,我有一些非常复杂的嵌套注释,到目前为止我只能匹配内部注释,这是我的注释的示例

@annotationA(property1 = "",
property2 = "",
property3 = "",
property4 = "",
property5 = "")
public class ClassA {

@annotationB(property1 = @annotationA(property5 = "anyChar",
property6 = false,
property1 = "anyChar",
property2 = "anyChar",
property3 = "anyChar",
property4 = "anyChar"),
params = { @annotationC(property7 = @annotationA(property5 = "anyChar"), property8 = @annotationA(property5 = "anyChar")),
@annotationC(property7 = @annotationA(property5 = "anyChar"), property8 = @annotationA(property5 = "anyChar")),
@annotationC(property7 = @annotationA(property5 = "anyChar"), property8 = @annotationA(property5 = "anyChar")),
@annotationC(property7 = @annotationA(property5 = "anyChar"), property8 = @annotationA(property5 = "anyChar")),
@annotationC(property7 = @annotationA(property5 = "anyChar"), property8 = @annotationA(property5 = "anyChar")),
@annotationC(property7 = @annotationA(property5 = "anyChar"), property8 = @annotationA(property5 = "anyChar")),
@annotationC(property7 = @annotationA(property5 = "anyChar"), property8 = @annotationA(property5 = "anyChar")),
@annotationC(property7 = @annotationA(property5 = "anyChar"), property8 = @annotationA(property5 = "anyChar")),
@annotationC(property7 = @annotationA(property5 = "anyChar"), property8 = @annotationA(property5 = "anyChar")), },
returnType = @annotationA(property5 = "anyChar"))
//some methode

}

这是我的正则表达式(未转义):

@\w+\([\n\w\s=\-"\@,.*:// {\+ }\.;+]+\)

最佳答案

如果您尝试删除注释,并且您有一个可以查找非嵌套注释的正则表达式,那么您基本上就完成了。除非这是一个对性能敏感的任务,否则您可以一遍又一遍地迭代该正则表达式,每次删除它匹配的内容。删除内部注释后,将不再需要处理嵌套注释。当正则表达式无法匹配任何内容时结束该过程,您就完成了!

您的示例(在 Notepad++ 中)需要 3 个 ReplaceAll 才能完全清理,这看起来可以接受。

关于用于匹配多行嵌套注释的java正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32935674/

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