2013-07-10 3 views
0

Java 문자열 (json one)의 패턴과 일치 시키려고합니다. 이 패턴은 문자열에서 여러 번 일치하지만 다른 문자열을 포함하는 문자열과도 일치합니다. 예를 들어 설명해 보겠습니다.정규 표현식이 큰 문자열과 일치 할 때 서브 쿼리 받기

String json = "IRRELEVANT_TEXT{'/element|1717_todossavoy/480/': {item_url:'/element|1717_Lorem/64/', item_description: 'Lorem ipsum dolor sit amet'},'/element|1717_Marcrie/480/': {item_url:'/element|1717_Vestibulum/64/', item_description: ' Vestibulum enim tellus, sodales sit amet consequat ut'},'/element|1717_Cannes05/434/': {item_url:'/element|1717_Nullam/64/', item_description: 'Nullam gravida risus vehicula nisi egestas'},'/element|1717_babelsavoy/266/': {item_url:'/element|1717_Pellentesque/64/', item_description: 'Pellentesque habitant morbi tristique senectus'}};IRRELEVANT"; 
    Matcher matcher = Pattern.compile("/element.*480/").matcher(json); 
    while(matcher.find()) { 
     System.out.println(matcher.group()); 
    } 

나는 점점 오전 다음

/element|1717_todossavoy/480/': {item_url:'/element|1717_Lorem/64/', item_description: 'Lorem ipsum dolor sit amet'},'/element|1717_Marcrie/480/ 

그러나 나는이 다음 키 좀하고 싶습니다 : 내가 잘못 뭐하는 거지

/element|1717_todossavoy/480/ 
/element|1717_Marcrie/480/ 

를?

+2

입니다. 욕심이없는 정규식의 그럴 것입니다. 'element. *? 480' 문제를 찾으려면 http://stackoverflow.com/a/2301298/891391 – yatul

답변

4

.*욕심하고 가능한만큼 일치하도록 노력할 것입니다. 당신은 물음표를 덧붙임으로써 그것을 꺼리고 /하지 않을 수 있습니다. .*?.

그러나이 은 여전히 ​​이므로 /element|1717_Lorem/64/480과 일치하므로 원하는 것을 제공하지 않습니다. 더 나은 정규식은 아마

/element[^/]+/480/ 
+1

+1을보십시오 !! – Kent

+0

답변 해 주셔서 감사합니다. 그것은 내 하루를 구했고, 그것은 정말로 understered :) – y2josei

관련 문제