2012-11-12 3 views
5

에서 나는 다음에 대한 정규식 찾기 위해 노력하고 있습니다 : WORD이 단어가 될 수 정확히 @[email protected] 일치Java 및 lookbehind 정규식

모든 세계가 전혀하지만 만 = 후됩니다. 나는 다음과 같은 제작 : 만 [email protected]@ 또는 @[email protected]하지만 = @[email protected]에 대한하지 같은 패턴을하지만, 작동
(?<==)#.*?#).
아니요 다음에 =이 뒤따라야하지만 그 사실을 알 수는 없습니다.
어쨌든 (?<=\\s*=\\s*)#.*?#)과 같은 것을 사용하지만 작동하지 않습니다.
아이디어가 있으십니까?

참고 : 이상한하지만 here에서 가변 길이 lookbehind 자바에서 지원 하지 것을 말한다 그러나 이것은 선택적인 공백

답변

0

이 패턴은 등호와 일치하는 예외 및 쌌다 단어를 나에게 제공하지 않습니다 @ 기호로 표시 :

Pattern pattern = Pattern.compile("= [email protected](.*)@"); 
Matcher matcher = pattern.matcher("[email protected]@"); 
if (matcher.matches()) { 
    System.out.println(matcher.group(1)); 
} 

// Prints: "WORD" 

내가 원하는 작업을 오해하지 않는 한, 둘러보기가 필요하지 않습니다. 그러나, 다음과 같은 작동합니다 :

Pattern pattern = Pattern.compile("(?<== ?)@(.*)@"); 
Matcher matcher = pattern.matcher("= @[email protected]"); 
if (matcher.find()) { 
    System.out.println(matcher.group(1)); 
} 

// Prints: "WORD" 

부정적인 패턴이 다음 수행됩니다 다음과 같이 당신이 보는 숨김 사용하는 경우

Pattern pattern = Pattern.compile("(?<!= ?)@(.*)@"); 
Matcher matcher = pattern.matcher("[email protected]@"); 
System.out.println(matcher.find()); 

// Prints: "false" 
1

것은, 내가 직접 PatternMatcher를 사용하고 있으리라 믿고있어, 단어를 깨끗하게 잡으려면 ("= @[email protected]" 대신 "@[email protected]").

그런 경우가 참으로 경우, 당신이해야 할 모든 내 옵션 공백을 추가 할 것입니다 모양 숨김

여기 (?<==\\s?)@.*[email protected]


"@[email protected]"를 반환하는 테스트 코드 :

Matcher m = Pattern.compile("(?<==\\s?)@.*[email protected]").matcher("= @[email protected]"); 
m.find(); 
System.out.println(m.group());