1

I used two time from java Pattern to get a specific value. It work fine in first one but not in second.

my code

public static void main(String[] args) throws IOException {
    final String path = "E://farhad.txt";
    FileReader fr = new FileReader(path);
    BufferedReader br = new BufferedReader(fr);
    String line;
    Pattern pattern = Pattern.compile("\\bCBM_Component_", Pattern.CASE_INSENSITIVE);
    while ((line = br.readLine()) != null) {
        Matcher matcher = pattern.matcher(line);
        String[] splitArray = new String[3];
        while (matcher.find()) {
            int i = 0;
            for (String retval : line.split("\"", 3)) {
                splitArray[i] = retval;
                i++;
            }
            System.out.println("CBM name: " + splitArray[1]);
            System.out.println("CBM number: " + splitArray[2].substring(2, splitArray[2].length() - 1));

            line = br.readLine();
            pattern = Pattern.compile("\\bAccountability:\\b", Pattern.CASE_INSENSITIVE);
            matcher = pattern.matcher(line);
            while (!matcher.find()) {
                line = br.readLine();
            }
        }
    }
    fr.close();
}

}

my document

View of the Model "CBM_Component_Account Reconciliation" (CBP10609)
===================================================================
      CBM Component
      Accountability: Execute
      Control Element:  Accounting Entity to Accounting Entity
      Relationship

from first pattern (CBM_Component_) it work fine but in second one (Accountability:) it doesn't do any thing. what I have to do. thank you

helderdarocha
  • 23,209
  • 4
  • 50
  • 65
farhad
  • 373
  • 2
  • 14
  • 28
  • 2
    What do you mean with "doesn't do anything"? Could you be more precise? Include the desired output (although it currently seems to be the desired outcome, that "Accountability" doesn't contribute to the output, at least I don't see a `println` statement for it). – fabian Jun 14 '14 at 14:55

1 Answers1

1

The question boils down to - why does the Pattern.compile("\\bAccountability:\\b", Pattern.CASE_INSENSITIVE) does not match the text: Accountability: Execute

This is because you're using word boundary matcher \b incorrectly (at the end of the pattern after the ":" character). When you'll remove the boundary matcher from the end of your pattern and you will get results that you expect.

More information on how the word boundary matcher works: Use of \b Boundary Matcher In Java

Community
  • 1
  • 1
kopytko
  • 203
  • 1
  • 6
  • I removed \\b after":" but it didn't match.I found second pattern doesn't work because when I use ("\\bAccountability:", Pattern.CASE_INSENSITIVE) in first pattern It worked fine – farhad Jun 15 '14 at 04:20