• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ExamLab, test 1, question 60 - Pattern problem

 
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers!

I've got some problems with understanding the following example from ExamLab 1 practice exam. Question no. 60:

The target is to print ">Java<" on the screen after execution of the code. One of the possible answers is this:



I've checked it, and it really doesn't print ">Java<" but ">Jav<" (it 'ate' the last letter - a).

Please, point me where am I making a mistake. I thought that this pattern will search for at least one word ([\\w+] following by a whitespace character ([\\s]) or digit ([\\d]).

So I thought it will work like this:

Certificate
on
Java2
(...)

because:
"Certificate " fits [\\w+][\\s]
"on " fits [\\w+][\\s]
"Java2" fits [\\w+][\\d]

so can anyone explain me why does this pattern 'eats' the last letters and it spits out the
Certificat
o
Jav
?

Thanks in advance!
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The pattern used is to remove the delimiters following structure "one or more characters ([A-Za-z0-9_]) followed by either digit or space"... So

Certificate on Java2 - J2SE

1-> "Certificate "---> "e " satisfies "\\w\\s"... So "Certificat"
2-> "on "---> Above said condition ... So "o"
3-> "Java2"---> "a2" = "\\w\\d" ... So "Jav"
4->" - J2SE"---> Here "J2" = "\\w\\d", "J2" will be removed and in the end, gives out two tokens " - " and "SE"...

So you can try for other pattern which removes only "spaces and digits which comes at the end of word" instead...
 
Piotr Nowicki
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aah, so the
[\\w+] means one or more CHARACTER from a word - not a one or more WORD's (that would be just \\w+ - without the parentheses).

I see that I've mixed the separation characters with pattern to find also.

Great thanks for your time Ram Narayan.M!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic