Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Harry Henriques
Ranch Hand
Posts: 206
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Harry Henriques
Ranch Hand
Posts: 206
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Harry Henriques
Ranch Hand
Posts: 206
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic