Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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_Regex - Fatal编程技术网

Java模式和匹配器没有结果

Java模式和匹配器没有结果,java,regex,Java,Regex,我写这个Java程序是为了好玩,我试图用一个正则表达式得到一组子字符串。 我的字符串是从文件中读取的,它类似于: \tJohn 其中\t是一个选项卡 我编写这段代码是为了隔离所需的子字符串,但在联机测试时,它工作正常,在我的代码中显示“没有匹配项” 试试看{ Pattern r=Pattern.compile(“.+(.+)”); 匹配器m=r.匹配器(线); 字符串名称=m.group(1); 字符串类型=m组(2); 字符串值=m.group(3); System.out.println(“

我写这个Java程序是为了好玩,我试图用一个正则表达式得到一组子字符串。 我的字符串是从文件中读取的,它类似于:

\tJohn

其中\t是一个选项卡

我编写这段代码是为了隔离所需的子字符串,但在联机测试时,它工作正常,在我的代码中显示“没有匹配项”

试试看{
Pattern r=Pattern.compile(“.+(.+)”);
匹配器m=r.匹配器(线);
字符串名称=m.group(1);
字符串类型=m组(2);
字符串值=m.group(3);
System.out.println(“\”+行+“\”匹配正则表达式”);
}捕获(例外情况除外){
System.out.println(“\”+行+“\”与正则表达式不匹配);
}
我的输出是:

"   <firstName type="String">John</firstName>" didn't match regex
"   <surname type="String">Doe</surname>" didn't match regex
"   <age type="int">18</age>" didn't match regex
“John”与regex不匹配
“Doe”与正则表达式不匹配
“18”与正则表达式不匹配

你有什么线索吗?

你刚刚在这行中创建了一个匹配器:
matcher m=r.matcher(line)
。如果打印异常,您将看到
未找到匹配项
消息

您正在使用
m.group()
而没有调用
matches()
方法

试试这个:

try {

    Pattern r = Pattern.compile(".+<(.+) type=\\\"(.+)\\\">(.+)</(.+)>");
    Matcher m = r.matcher(line);
    if (m.matches()) {

        String name = m.group(1);
        String type = m.group(2);
        String value = m.group(3);
        System.out.println("\"" + line + "\" matched regex");
    }

} catch (Exception ex) {
    System.out.println("\"" + line + "\" didn't match regex");
}
试试看{
Pattern r=Pattern.compile(“.+(.+)”);
匹配器m=r.匹配器(线);
如果(m.matches()){
字符串名称=m.group(1);
字符串类型=m组(2);
字符串值=m.group(3);
System.out.println(“\”+行+“\”匹配正则表达式”);
}
}捕获(例外情况除外){
System.out.println(“\”+行+“\”与正则表达式不匹配);
}

您刚刚在这一行中创建了一个匹配器:
匹配器m=r.matcher(line)
。如果打印异常,您将看到
未找到匹配项
消息

您正在使用
m.group()
而没有调用
matches()
方法

试试这个:

try {

    Pattern r = Pattern.compile(".+<(.+) type=\\\"(.+)\\\">(.+)</(.+)>");
    Matcher m = r.matcher(line);
    if (m.matches()) {

        String name = m.group(1);
        String type = m.group(2);
        String value = m.group(3);
        System.out.println("\"" + line + "\" matched regex");
    }

} catch (Exception ex) {
    System.out.println("\"" + line + "\" didn't match regex");
}
试试看{
Pattern r=Pattern.compile(“.+(.+)”);
匹配器m=r.匹配器(线);
如果(m.matches()){
字符串名称=m.group(1);
字符串类型=m组(2);
字符串值=m.group(3);
System.out.println(“\”+行+“\”匹配正则表达式”);
}
}捕获(例外情况除外){
System.out.println(“\”+行+“\”与正则表达式不匹配);
}

这看起来像一个for循环。由于直接访问不存在的列表元素,您可能处于NullPointer或IndexOutofBond中。如果你一行一行地解析,每次只有一个组。你在哪里在线测试它?这看起来像一个for循环。由于直接访问不存在的列表元素,您可能处于NullPointer或IndexOutofBond中。如果你一行一行地解析,你每次只能有一个组。你在哪里在线测试呢?太好了,谢谢!我不知道m.matches是强制性的。非常感谢。太好了,谢谢!我不知道m.matches是强制性的。非常感谢。