Java 特定标记之间的每个单词调用函数

Java 特定标记之间的每个单词调用函数,java,arrays,regex,string,for-loop,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

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

(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页,但没有找到任何有用的东西。

使用
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));
}

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

这是示例代码

输出

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.
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) {
        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
    }
}
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
    }
}
代码

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
    }
}
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
    }
}
import java.util.regex.*;
导入java.util.*;
公共类HelloWorld{
公共静态void main(字符串[]args){
int指数=0;
Map allMatches=newhashmap();
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));
对于(Map.Entry match:allMatches.entrySet())
myFunction(match.getKey(),match.getValue());
}
公共静态void myFunction(int索引,字符串ip){
System.out.println(“索引:“+Index+”-获取字符串“+ip+”,以便进一步处理”);
//你在这里处理吗
}
}

您只需获取子字符串并使用
\)\(
简单正则表达式:

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

查看

您没有使用正确的关键字。为什么不搜索“split string”之类的内容?您希望在JSP或Java中使用它吗?是否也可以为它提供某种索引?公共静态void myFunction(string string,int index){}将如下所示,并且它会为每个“ip”递增地址。所以:我想举例说:
ip0;ip1;ip2;IP3;
@Jason如果你也想存储索引,那么你可以使用
Map
而不是
List
。查看我的更新答案(编辑1)