• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)



 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic