Metacharacter \B matches (OCP exam)
Asked Answered
K

1

8

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

Komsomolsk answered 25/7, 2015 at 18:28 Comment(5)
ya, 2 one is the correct one. Does "\B" would be passed as \\B ?Tales
@AvinashRaj Command-line input does not have to be escaped, so "\B" is equal to a Java String "\\B" passed in directly in the code.Komsomolsk
mistake by the authors of the study guide If you run the code and get the same result, it must be considered the claim is in error. Regex engine primitives don't change, in fact \B means not-word boundary on probably every engine. So, is it possible the claim is in error ? I'd say so..Petronilapetronilla
@sln Thanks. These kind of exams have a lot of trick questions, so you start to doubt everything you see. My only hope is they have thoroughly debugged the actual exam questions.Komsomolsk
I have an exam for your exam makers. They should send all their questions to me to check their work.Petronilapetronilla
C
6

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.

Carney answered 25/7, 2015 at 19:48 Comment(3)
You are completely right! Why would they put something like that in the exam? It's trick questions like these that I hate, because you're not really testing someone's knowledge of Java. Thanks for figuring this one out!Komsomolsk
@Neftas: my hypothesis is authors have included the "$" in the question, executed the program and copied the answer to the book, without see the "special" meaning of this symbol. Well, at least, they have tested the examples, a lot of books doesn't ;-)Carney
Yes, and I was testing the code using eclim (which uses a headless Eclipse instance), so I bypassed the shell while passing arguments to the program... It all figures, mystery solved :)Komsomolsk

© 2022 - 2024 — McMap. All rights reserved.