What is the result?

Given the code fragment:

String s = “Java 7, Java 6”;

Pattern p = Pattern.compile(“Java.+\\d”);

Matcher m = p.matcher(s);

while (m.find()) {

System.out.println(m.group());

}

What is the result?

Given the code fragment:

String s = “Java 7, Java 6”;

Pattern p = Pattern.compile(“Java.+\\d”);

Matcher m = p.matcher(s);

while (m.find()) {

System.out.println(m.group());

}

What is the result?

A.
Java 7

B.
Java 6

C.
Java 7, Java 6

D.
Java 7
java 6

E.
Java



Leave a Reply 6

Your email address will not be published. Required fields are marked *


Francesco

Francesco

The correct asnwer is D as it finds the 2 patterns and print eachone ona separate line as we have println()

Yury

Yury

No, answer C is correct as there is a greedy quantifier of any character (.+)

ses

ses

run:
Java 7, Java 6
BUILD SUCCESSFUL (total time: 1 second)
——————
compile it:
—————-
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Frage118 {
public static void main(String[] args) {
String s = “Java 7, Java 6”;
Pattern p = Pattern.compile(“Java.+\\d”);
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group());
}
}
}

Charlie

Charlie

C. ” 7, Java ” matches “.+” so the whole string matches “Java.+\\d”.

Elaine

Elaine

Thank you!

pavan

pavan

“Java.+\\d” indicates string starting with “Java” and ending with any digit

C is correct