Which regular expression, inserted at line 12, correctly splits test into "Test A", "Test B", and "Test C"?

Given:

11. String test = “Test A. Test B. Test C.”;
12. // insert code here
13. String[] result = test.split(regex);

Which regular expression, inserted at line 12, correctly splits test into “Test A”, “Test B”, and “Test
C”?

Given:

11. String test = “Test A. Test B. Test C.”;
12. // insert code here
13. String[] result = test.split(regex);

Which regular expression, inserted at line 12, correctly splits test into “Test A”, “Test B”, and “Test
C”?

A.
String regex = “”;

B.
String regex = ” “;

C.
String regex = “.*”;

D.
String regex = “\\s”;

E.
String regex = “\\.\\s*”;

F.
String regex = “\\w[ \.] +”;



Leave a Reply 2

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


Matti

Matti

E is correct. String is split by a regex, which has one point and optional space character.

Sathya janaki

Sathya janaki

Explanation:

Remember that if you need to create a String that contains a double quote ” or a backslash \ you need to add an escape character first.If you need to search for periods (.) in your source data and If you just put a period in the regex expression, you get the “any character” behavior.

So, what if you try \. ? Now the Java compiler thinks you’re trying to create an escape sequence that doesn’t exist.

The correct syntax to do so is
String s = “ab.cde.fg”;
String[] tokens = s.split(“\\.”);
which gives Output as :
abcdefg

Removes .(dot) and joins them.

Similarly \\s refers any occurrence of White Space and \\s* refers to occurrence of White Space character zero or more times .

String regex = “\\.\\s*”; is used which tells compiler that when both .(dot) and any number of White Space occurs together then remove them .Therefore output will be Test ATest BTest C .