• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

regexp question

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I don't understand the result of this code:



Result is
2 oL
5 oL
9 uL

If I were the jvm I'd behave like that:
look for a "C" (which I find at index 0)
look if it's followed by any 0 or more wildcard characther (I see two "o" at position 1 - 2)
And at position 3 there's a nice "L"
So the first println should be:
0 CooL

But what's the real and correct way of thinking of Jvm?
Thank you guys.
Gianni.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

The pattern is "C.L" means .....find C (0 or more) then .(any char)and then followed by L, so .....in "CooLooLCuuLooC". I think now you can understand that your result is correct ..
Result is
2 oL
5 oL
9 uL
 
gianni ipez
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, I got you.
but then, what about the following question (enthuware - com.enthuware.ets.scjp.v5.2.120):

A programmer has writted the following regular expression.

C*.L

Identify the words that it will capture in the input string:

CooLooLCuuLooC

1) CooL and CuuL
2) CooLooL and CuuL
3) CooLooLCuuL
4) None of these.

The answer given is 3).
I still don't understand why.
thanks,
Gianni
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you mean C.*L?
 
gianni ipez
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
on the mock I mentioned there was C*.L
So is it a mock error?
thank you
 
gianni ipez
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I checked, the regexp is really: C*.L

So should we assume the mock answer is not correct?

thank u
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given that question, C*.L will not match any of those options.

But if the regular expression is C.*L, then it will match 3.
 
gianni ipez
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a bit disappointed since I spent 2 hours trying to figure out a behaviour that was uncorrect! by the way, now I can understand.

should we move the thread in mock errata forum?
I inform enthuware about the problem with this question.
Thank you.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

I think the first example is an example of a programmer who mixed something up.

Keith already pointed into that direction.

[drum roll]
C.*L versus C*.L
[/drum roll]


The first (C.*L) finds patterns flanked by C and L. Looks much like a nice little pattern. Could be better if reluctant (C.*?L) but works.

The second (C*.L) finds:
Zero (!!!) or multiple occurences of "C",
then any character (one only)
and the third (or second, third... depending on how many "C"s are there) character is an "L".

Or: it finds only junk.
The "C" doesn't matter at all (zero or many "C"s)



Perhaps test this variation(s):


Yours,
Bu.
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic