2017-09-21 2 views
0

일부 코드에 대한 stylechecker를 작성하는 노력과 여러 정규식 자바에서 backrefrences를 사용하여 ... 여기에 정규식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 

내 예상 경기는 @Import 문에서 SomeFactory.class 될 것이지만,이 데리러하지 않습니다 내 예제 코드는 다음과 같습니다?

당신이 정규식 사용할 수 있습니다
+1

패턴을 컴파일 할 때'Pattern.DOTALL' 플래그를 사용하거나 잊지 마십시오. – Zefick

+1

당신은 당신의 예상 경기를 추가 할 수 있습니까? –

답변

1

:

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

RegEx Demo

캡처 그룹 # 1 가져 오기 선 후 SomeFactory.class 및 캡처 그룹 # 2이 후 SomeFactory.class입니다 : 자바 사용에

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

@Import 행.

+0

@backwardsboy : 이것이 해결되지 않으면 알려주세요. – anubhava