javaregexocpjp

Metacharacter \B matches (OCP exam)


I am studying for the Java OCP certificate. I am taking mock exams to prepare.

Example program:

public class Quetico {
    public static void main(String[] args) {
        Pattern p = Pattern.compile(args[0]);
        Matcher m = p.matcher(args[1]);
        while (m.find()) {
            System.out.println(m.start() + " ");
        }
        System.out.println("");
    }
}

the authors of the OCA/OCP Jave SE 7 Study Guide maintain that the execution:

java Quetico "\B" "^23 *$76 bc"

will produce the output

0 2 4 8

However, when I run the code from Eclipse or test it on an outside source, I get

0 2 4 5 7 10

Am I missing something here, or is it a mistake by the authors of the study guide?

I am adding the actual question from the book below for reference.

Question from the Book

OCP Java 7 Self Test Question 8.3

Answer

enter image description here


Solution

  • The book is correct (when executing over a Unix machine with the usual shells). It is a combination of shell behaviour and java (in my opinion, off-topic to a course of Java). Remember "$" in shell means replacement. So, if you call the program as:

    java Quetico "\B" "^23 *$76 bc"
    

    the string that is matched over regex is (you can add a println for args[1] to verify it):

    ^23 *6 bc
    

    with the result given by the book "0 2 4 8".

    You can compare the result with the one of:

    java Quetico "\B" '^23 *$76 bc'
    

    that disables shell substitution.