This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
This expression was listed as one of the correct choices in ExamLab for a String.split() delimiter where one or more whitespace characters separate the tokens. The brackets [ ] denote a regex character class.
I think the correct expressions should be one or both of the following:
What do you think?
The Java Tutorials on Regular Expressions wrote:A regex character class is a set of characters enclosed within square brackets. It specifies the characters that will successfully match a single character from a given string
[abc] a, b, or c (simple class)
[^abc] Any character except a, b, or c (negation)
[a-zA-Z] a through z or A through Z, inclusive (range)
[a-d[m-p]] a through d, or m through p: [a-dm-p] (union)
[a-z&&[def]] d, e, or f (intersection)
[a-z&&[^bc]] a through z, except for b and c: [ad-z] (subtraction)
[a-z&&[^m-p]] a through z, and not m through p: [a-lq-z](subtraction)
In the first two cases, nothing is split, as there are no delimiters that has an open paren, a bunch of spaces, and a close paren -- split returns an array of size one, which contains the original string.
In the last case, the delimiter is just one or more spaces, so it does split it into a bunch of words.
Oops... Small correction. Only the second example's delimiter is an open parens, a bunch of spaces, and a close paren. The first example's delimiter is an open paren, one space, and a bunch of close parens. Regardless, neither case will match a delimiter for the string, and nothing will be split.