0

I have a long string in this format (a long single line in file):

"1":"Aname","2":"AnotherName","3":"Sempronio"

I want to extract the number and the name and save them on a Map.

I tried this:

    FileReader fileReader = null;
    BufferedReader br = null;

    File file = new File("./SingleLineFileNames.txt");
    try {
        fileReader = new FileReader(file);
        br = new BufferedReader(fileReader);

        String line;
        Pattern p = Pattern.compile("\"(\\d+)\":\"([\\w-.' ]+)\"");
        Matcher matcher;
        while((line = br.readLine()) != null) {
            matcher = p.matcher(line);

            String name;
            int i = 1;
            while((name = matcher.group(i)) != null){ 
                // save in map
                i++;
            }

        }

    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    finally {
        try {
            br.close();
            fileReader.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;

result is java.lang.IllegalStateException: No match found

It's the right way to iterate on groups?

Where I wrong?

1
  • Are you working on JSON? Use a proper parser for that, please. Commented Mar 7, 2014 at 11:27

3 Answers 3

1

First split the String at , (String#split) and then split each resulting array element at : to get key and value. With input strings like these, I wonder what kind of masochism is on the developers using regex sledgehammers breaking these simple nuts..

Sign up to request clarification or add additional context in comments.

1 Comment

I guess that regex is a most elegant way to parse this string.
0

If you use hyphen inside [] then always place at the first or at the last.

Pattern p = Pattern.compile("\"(\\d+)\":\"([-\\w.' ]+)\"");
                                            ^ here

Also the way you are checking the group() is not correct. Check here:

while(matcher.find()){ 
    System.out.println(matcher.group(1));
    System.out.println(matcher.group(2));
}

2 Comments

It works both with Pattern p = Pattern.compile("\"(\\d+)\":\"([\\w-.' ]+)\""); and with Pattern.compile("\"(\\d+)\":\"([-\\w.' ]+)\"");. Why, Sabuj, you say that hypher always place at the first or at the last?
For example this one [a-z] it means any character from a to z. So placing hyphen at the middle can change it's meaning. So its always a good practice to use it at start of end in the []
0

Remove the broken square bracket construct ([\\w-.' ]+) . For the name containing word characters only, it is enough to put (\\w+) there.

1 Comment

names substrings may contain -, ` ` and .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.