2016-09-09 2 views
2

괄호 안에있는 모든 단어를 인쇄하고 싶습니다. 예를 들어 내 문자열은 다음과 같습니다특정 태그 간의 foreach 단어는 함수 호출

(192.168.0.112)(192.168.0.166)(192.168.0.112)(192.168.0.166)(192.168.0.106) 

내가 for 루프에서 원하는,이 일이 일어날 수 있도록. 나는 어떤 종류의 for 루프를 사용해야 만한다는 것을 알고 있지만 어떻게 해야할지 모른다.

myFunction(192.168.0.112); 
myFunction(192.168.0.166); 
myFunction(192.168.0.112); 
myFunction(192.168.0.166); 

Google에서 많은 페이지를 검색했으며 작동하지 않았습니다.

+0

귀하는 올바른 키워드를 사용하지 않았습니다. "split string"과 같은 것을 검색하지 않는 이유는 무엇입니까 –

+0

일부 JSP 또는 Java에서이 작업을 원합니까? –

답변

3

당신은 괄호 안에 모든 일들을 캡처하는 정규 표현식 \(([^)]*)\)을 사용할 수 있습니다

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
... 
Matcher m = Pattern.compile("(?<=\\().*?(?=\\))").matcher(str); 
while (m.find()) { 
    myFunction(m.group(0)); 
} 
. 난을 저장하는 -

은 예제 코드를

출력

got string 192.168.0.112 for further processing. 
got string 192.168.0.166 for further processing. 
got string 192.168.0.112 for further processing. 
got string 192.168.0.166 for further processing. 
got string 192.168.0.106 for further processing. 

코드

import java.util.regex.*; 
import java.util.*; 

public class HelloWorld { 
    public static void main(String[] args) { 
     List <String> allMatches = new ArrayList <String>(); 
     Matcher m = Pattern.compile("\\(([^)]*)\\)") 
      .matcher("(192.168.0.112)(192.168.0.166)(192.168.0.112)(192.168.0.166)(192.168.0.106)"); 

     while (m.find()) 
      allMatches.add(m.group(1)); 

     for (String match: allMatches) 
      myFunction(match); 
    } 

    public static void myFunction(String string) { 
     System.out.println("got string " + string + " for further processing."); 
     //do your processing here 
    } 
} 

EDIT 1 ndex 또한, 대신 List

출력

Index: 0 - got string 192.168.0.112 for further processing. 
Index: 1 - got string 192.168.0.166 for further processing. 
Index: 2 - got string 192.168.0.112 for further processing. 
Index: 3 - got string 192.168.0.166 for further processing. 
Index: 4 - got string 192.168.0.106 for further processing. 

코드의 Map을 사용할 수 있습니다 당신은 단지 \)\( 간단한 정규식 문자열 및 분할 얻을 수

import java.util.regex.*; 
import java.util.*; 

public class HelloWorld { 
    public static void main(String[] args) { 
     int index=0; 
     Map<Integer, String> allMatches = new HashMap<Integer, String>(); 
     Matcher m = Pattern.compile("\\(([^)]*)\\)") 
      .matcher("(192.168.0.112)(192.168.0.166)(192.168.0.112)(192.168.0.166)(192.168.0.106)"); 

     while (m.find()) 
      allMatches.put(index++, m.group(1)); 

     for (Map.Entry<Integer, String> match: allMatches.entrySet()) 
      myFunction(match.getKey(), match.getValue()); 
    } 

    public static void myFunction(int index, String ip) { 
     System.out.println("Index: " + index + " - got string " + ip + " for further processing."); 
     //do your processing here 
    } 
} 
+0

일종의 색인을 제공하는 것도 가능합니까? 그 정적 정적 무효 myFunction (문자열 문자열, int 인덱스) {}이 모양과 각 "IP"주소에 대해 증가합니다. 예를 들면 다음과 같습니다.'IP 0; IP 1; IP 2; IP3;' – Jason

+0

@ Jason 인덱스를 저장하고 싶다면'List' 대신'Map'을 사용할 수 있습니다. 내 업데이트 답변보기 (* Edit 1 *) –

4

함수와 함께 Matcher 클래스를 사용하면 모든 일치 항목을 찾아 반복 할 수 있습니다.

DEMO

또는

Matcher m = Pattern.compile("\\((.*?)\\)").matcher(str); 
while (m.find()) { 
    myFunction(m.group(1)); 
} 
0

:

String s = "(192.168.0.112)(192.168.0.166)(192.168.0.112)(192.168.0.166)(192.168.0.106)"; 
String[] res = s.substring(1, s.length()-2).split("\\)\\("); 
System.out.println(Arrays.toString(res)); 

Java demo