某些标签之间的Foreach字呼叫功能

时间:2016-09-09 06:36:46

标签: java arrays regex string for-loop

我想要打印出括号内的所有单词。例如,我的字符串如下所示:

(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);

我在谷歌搜索了很多,大约9页,我还没有找到任何工作的东西。

3 个答案:

答案 0 :(得分:4)

使用Matcher类和while函数来查找和迭代所有匹配项。

import java.util.regex.Matcher;
import java.util.regex.Pattern;
...
Matcher m = Pattern.compile("(?<=\\().*?(?=\\))").matcher(str);
while (m.find()) {
    myFunction(m.group(0));
}

DEMO

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

答案 1 :(得分:3)

您可以使用正则表达式\(([^)]*)\)捕获括号内的所有内容。

这是示例代码

<强>输出

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
    }
}

编辑1 - 也可以存储索引,您可以使用Map代替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.

<强>代码

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
    }
}

答案 2 :(得分: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