Java 正则表达式-非法重复?

Java 正则表达式-非法重复?,java,regex,Java,Regex,在Java中使用正则表达式。我一直试图让它工作,但它每次都抛出该死的错误。我确信这和花括号有关 String openbrace = Pattern.quote("{"); String closebrace = Pattern.quote("}"); Pattern pattern = Pattern.compile(openbrace+"[ ]?\"(.*?)\"[ ]?,[ ]?\"(.*?)\"[ ]?"+closebrace); + = 编辑:我正在将NetBeans 7.0与JDK

在Java中使用正则表达式。我一直试图让它工作,但它每次都抛出该死的错误。我确信这和花括号有关

String openbrace = Pattern.quote("{");
String closebrace = Pattern.quote("}");
Pattern pattern = Pattern.compile(openbrace+"[ ]?\"(.*?)\"[ ]?,[ ]?\"(.*?)\"[ ]?"+closebrace);
+

=

编辑:我正在将NetBeans 7.0与JDK 1.7一起使用,
“\\{\\s*\”(.*?“\\s*”,\\s*”(.*?“\\s*\”)(.*?“\\s*\”
怎么样

我刚刚编译并运行了以下程序。正确运行:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class App
{
    public static void main(String[] args)
    {
        Pattern p = Pattern.compile("\\{\\s*\"(.*?)\"\\s*,\\s*\"(.*?)\"\\s*\\}");
        Matcher m = p.matcher("{ \"working\", \"working\"}");

        while(m.find())
        {
            System.out.println(m.start(1) + " - " + m.end(1));
            System.out.println(m.start(2) + " - " + m.end(2));
        }
    }
}

只是
Pattern.compile(“[]?\”(.*?\”[]?,[]?\”(.*?\“[]?”)
compile?是的。正如我所说的-这一定是那些大括号。关于
模式。编译(“\{[]]?\”(.*?\”[]?,[]?\”(.*?\”)(.*?\“[]?\”)
?检查这些字符串的内容。应该是
\{
\}
。为什么需要将空格括在方括号中?您的模式会导致非法重复错误。字符串“closebrace”作为“\Q}\E”输出到控制台。“非法转义字符”表示NetBeans IDE它是否没有openbrace和closebrace?
Illegal Repetition
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class App
{
    public static void main(String[] args)
    {
        Pattern p = Pattern.compile("\\{\\s*\"(.*?)\"\\s*,\\s*\"(.*?)\"\\s*\\}");
        Matcher m = p.matcher("{ \"working\", \"working\"}");

        while(m.find())
        {
            System.out.println(m.start(1) + " - " + m.end(1));
            System.out.println(m.start(2) + " - " + m.end(2));
        }
    }
}