aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ExamLab: invalid expression  String [] s = files.split( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ExamLab: invalid expression  String [] s = files.split("[(\\s)+]");" Watch "ExamLab: invalid expression  String [] s = files.split("[(\\s)+]");" New topic
Author

ExamLab: invalid expression String [] s = files.split("[(\\s)+]");

Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
Hi,

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?

Harry

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


Pattern.html wrote:
Character classes
[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)


Harsh Pensi
Ranch Hand

Joined: Aug 05, 2009
Posts: 67
Harry Henriques wrote:Hi,

I think the correct expressions should be one or both of the following:


What do you think?

The given regex [(\\s)+] does not work as desired.
1. is correct. 2. has invalid escape sequence


SCJP6 - 93% SCWCD5 - 97%
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
You are correct, Harsh. The following has an invalid escape sequence.



How about this?



or this?



The output for the previous 2 snippets is:





The output for the last one is:



Do you see the difference? Why does it do this?

Regards, Harry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18745
    
  40

Do you see the difference? Why does it do this?


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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
Thanks, Henry. I see my mistake, now. It is not necessary to escape the parenthesis.





Both of the above regular expressions split the String into one word tokens. The result of both of these is the following.



Regards, Harry

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ExamLab: invalid expression String [] s = files.split("[(\\s)+]");