0

I currently have the following:

  int noOfRPs = 3;

            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(getAssets().open("wifi_log")));

                // There are 3 RP's

                for (int RP = 0; RP < noOfRPs; ++RP){

                        String line;
                        while ((line = br.readLine()) != null){
                            String pattern = "^RP" + RP;
                            Pattern p = Pattern.compile(pattern);
                            Matcher m = p.matcher(line);
                            if (m.find())
                                System.out.println("match = " + line);

                        }

                }

            }
            catch (IOException e){
                System.out.println("IO Error, no file found");
                e.printStackTrace();
            }

I want to match the patterns: RP0 then RP1 and finally RP2 in my text file. However at the moment, it is continuously matching RP0 despite me changing the variable pattern on each run of the for loop...

Would anyone know what is happening here?

Thanks for your help.

6
  • 1
    In the while loop RP doesn't change. Commented Aug 14, 2014 at 12:52
  • (1) You're compiling the regex for every line in the file, which doesn't make a lot of sense. (2) You iterate over the file lines... now you're at the end of the file. Now you increment RP, but you're still at the end of the file. (3) Are your loops inside-out? (4) Why not just compile three regexes and use each one? Commented Aug 14, 2014 at 12:53
  • @DaveNewton Thanks for your response. With regards to your second point, how could I fix this? I need to go over the file 3 times. The first time I am trying to extract lines with RP0.. the second time RP1 and the third time RP2. Commented Aug 14, 2014 at 13:07
  • Without knowing details, I'd argue you only need to go over the file once, checking each line for RP0/RP1/RP2, and stuff them into an appropriate collection (i.e., one for each RPn). Commented Aug 14, 2014 at 13:19
  • @DaveNewton In this example I need to use 3 regexes... However, I could end up having to use upwards of 50. It seems odd to have to create 50 different regex patterns manually? Commented Aug 14, 2014 at 14:10

2 Answers 2

1

That happens because the increment in RP (note, double-check variable conventions) is taking place one level above your while loop iteration.

A quick solution would be to invert the hierarchy of the loops, placing the for loop inside the while loop.

As such:

String line;
while ((line = br.readLine()) != null){
    for (int RP = 0; RP < noOfRPs; ++RP){
        String pattern = "^RP" + RP;
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(line);
        if (m.find())
            System.out.println("match = " + line);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You read the file only on the first iteration of your for loop. On later iterations br.readLine() will return null immediately and there won't be any matching done.

One idea would be to merge your three patterns into a single pattern and match for all three subpatterns at the same time. To achieve this, take a look at this question. In short it is possible to use a pipe (|) to combine patterns.

Comments

Your Answer

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