开发者

regex - illegal repetition?

Working with Regex in Java. I keep trying to get this to work, but it throws the damn error each and every time. I'm convinced it has to do with the curly braces.

String openbrace = Pattern.quote("{");
开发者_如何学PythonString closebrace = Pattern.quote("}");
Pattern pattern = Pattern.compile(openbrace+"[ ]?\"(.*?)\"[ ]?,[ ]?\"(.*?)\"[ ]?"+closebrace);

+

{ "Working", "Working" },

=

Illegal Repetition

EDIT: I am using NetBeans 7.0 with JDK 1.7


How about "\\{\\s*\"(.*?)\"\\s*,\\s*\"(.*?)\"\\s*\\}" ?

Have just compiled and run the following program. Runs correctly:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜