繁体   English   中英

多行Java正则表达式中的Backrefrences

[英]Backrefrences in multi-line java regex

尝试为某些代码编写样式检查器,并在Java中将backrefrences用于多行正则表达式...这是正则表达式的外观

.*import com.+([a-zA-Z]+Factory\\.class).*\\\\1.*

基本上,想在我的代码中找到工厂类的第一个实例。 我的示例代码如下所示:

import com.sample.OtherClass;
import com.sample.cool.SomeFactory.class;

// other nonsense

@Import(clazz = SomeFactory.class)
// other nonsense

我预计比赛将是SomeFactory.class@Import声明,但它不挑这件事......有什么建议?

您可以使用此正则表达式:

(?s)import\s+com.+?\.([a-zA-Z]+Factory\.class).*(\1)

在Java中使用:

final String regex = "(?s)import\\s+com.+?\\.([a-zA-Z]+Factory\\.class).*(\\1)";

正则演示

捕获组#1是SomeFactory.class进口线之后和捕获组#2是SomeFactory.class@Import线。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM