开发者

regex - illegal repetition?

开发者 https://www.devze.com 2023-03-20 05:49 出处:网络
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.

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

精彩评论

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