Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用组提取一组多个限制器之间的字符串_Java_Indexoutofboundsexception - Fatal编程技术网

Java 使用组提取一组多个限制器之间的字符串

Java 使用组提取一组多个限制器之间的字符串,java,indexoutofboundsexception,Java,Indexoutofboundsexception,正如标题所说,我有一个字符串,我想从中提取一些数据 这是我的字符串: text = "|tab_PRO|1|1|#tRecordType#||0|tab_PRO|"; 我想提取管道之间的所有数据:tab_PRO,1,1…等等 . 我试过: Pattern p = Pattern.compile("\\|(.*?)\\|"); Matcher m = p.matcher(text); while(m.find()) { for(int i = 1; i&l

正如标题所说,我有一个字符串,我想从中提取一些数据

这是我的字符串:

text = "|tab_PRO|1|1|#tRecordType#||0|tab_PRO|";
我想提取管道之间的所有数据:tab_PRO,1,1…等等 . 我试过:

Pattern p = Pattern.compile("\\|(.*?)\\|");
    Matcher m = p.matcher(text);
    while(m.find())
    {
        for(int i = 1; i< 10; i++) {
        test = m.group(i); 
        System.out.println(test);
        }
    }
通过这个,我得到了第一组tab_PRO。但我也犯了一个错误

java.lang.IndexOutOfBoundsException:无组2

现在,我可能不太了解这些小组是如何工作的,但我认为通过这些我可以得到我需要的剩余数据。我无法理解我错过了什么

提前感谢

尝试使用.split或.substring

尝试使用.split或.substring

使用。考虑到它需要一个正则表达式作为参数,而|是一个保留正则表达式操作数,因此需要用\来转义它。因此,将其设为两个\So\|不会被解释为对|字符使用了-invalid-escape序列:

String[] parts = text.split("\\|");
看到它在这里工作:

如果您想使用正则表达式方法,则需要对每个|之后的每个重复字符进行分组和捕获,并将它们限制为除|之外的任何字符,可能需要使用像\\\\\\\\\\\\\\\\\]*这样的正则表达式

在循环中,您迭代m.find并只使用capture group 1,因为它是每个匹配的唯一组

String text = "|tab_PRO|1|1|#tRecordType#||0|tab_PRO|";
Pattern p = Pattern.compile("\\|([^\\|]*)");
Matcher m = p.matcher(text);
while(m.find()){
    System.out.println(m.group(1));
}
使用。考虑到它需要一个正则表达式作为参数,而|是一个保留正则表达式操作数,因此需要用\来转义它。因此,将其设为两个\So\|不会被解释为对|字符使用了-invalid-escape序列:

String[] parts = text.split("\\|");
看到它在这里工作:

如果您想使用正则表达式方法,则需要对每个|之后的每个重复字符进行分组和捕获,并将它们限制为除|之外的任何字符,可能需要使用像\\\\\\\\\\\\\\\\\]*这样的正则表达式

在循环中,您迭代m.find并只使用capture group 1,因为它是每个匹配的唯一组

String text = "|tab_PRO|1|1|#tRecordType#||0|tab_PRO|";
Pattern p = Pattern.compile("\\|([^\\|]*)");
Matcher m = p.matcher(text);
while(m.find()){
    System.out.println(m.group(1));
}

如注释中所述,使用String.split更容易做到这一点

至于您自己的代码,您不必要地使用了内部循环,这导致了该异常。您只有一个组,但for循环将导致您查询多个组。您的循环应尽可能简单:

Pattern p = Pattern.compile("(?<=\\|)(.*?)\\|");
Matcher m = p.matcher(text);
while (m.find()) {
    String test = m.group(1);
    System.out.println(test);
}

请注意,我必须在正则表达式中使用look-behind断言。

如注释中所述,使用String.split更容易做到这一点

至于您自己的代码,您不必要地使用了内部循环,这导致了该异常。您只有一个组,但for循环将导致您查询多个组。您的循环应尽可能简单:

Pattern p = Pattern.compile("(?<=\\|)(.*?)\\|");
Matcher m = p.matcher(text);
while (m.find()) {
    String test = m.group(1);
    System.out.println(test);
}

请注意,我必须在正则表达式中使用look-behind断言。

您尝试过吗?您可以使用substring或split方法提取字符串的所需部分。您尝试过吗?您可以使用substring或split方法提取字符串的所需部分。