2009-11-13 3 views

답변

4

이 시도 (언어가 자바입니다) :

AA(?:(?!AA).)*END 

데모 :

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class Main { 
    public static void main(String[] args) { 
     String text = "This is AA and this is AA and this is AA and this is the END blah blah"; 
     Matcher m = Pattern.compile("AA(?:(?!AA).)*END").matcher(text); 
     while(m.find()) { 
      System.out.println("match ->"+m.group()+"<-"); 
     } 
    } 
} 

그리고 AAEND 사이에 줄 바꿈이있을 수있는 경우 (?s) (DOT-ALL 플래그를 추가) 당신의 정규식의 시작 부분에.

짧은 설명 :

AA   # match 'AA' 
(?:   # open non-capturing group 1 
    (?!AA). # if 'AA' cannot be seen, match any char (except line breaks) 
)*   # close non-capturing group 1 and repeat it zero or more times 
END   # match 'END' 
+0

귀하는 전문가입니다. 감사합니다 –

+0

리차드를 환영합니다. 예, 저는 몇 가지 정규식을 알고 있지만, 훨씬 더 많이 알고있는이 주변에는 꽤 많은 사람들이 있습니다! –

1

또 다른 대답은 :

str.substring(0, str.lastIndexOf("END")).lastIndexOf("AA"); 

이 "END"에 확장 문자열을 생성하고 그 문자열 내에서 검색 문자열이 발견되는 마지막 위치를 찾습니다.