Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex - illegal repetition?

Tags:

java

regex

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("{");
String closebrace = Pattern.quote("}");
Pattern pattern = Pattern.compile(openbrace+"[ ]?\"(.*?)\"[ ]?,[ ]?\"(.*?)\"[ ]?"+closebrace);

+

{ "Working", "Working" },

=

Illegal Repetition

EDIT: I am using NetBeans 7.0 with JDK 1.7

like image 201
guywhoneedsahand Avatar asked Jul 14 '11 23:07

guywhoneedsahand


1 Answers

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));
        }
    }
}
like image 83
Mike Mozhaev Avatar answered Oct 23 '22 03:10

Mike Mozhaev