Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Frustrated Sierra/Bates Guide OCP7 Self Test Question 1  RSS feed

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The answer is E: 01234456

I understand how they get to 01234. 2 is the third index of the number 34 which is returned by group(), but where on earth does the 456 come from? Especially the number 6 if the source ab34ef, which is zero-based, has only a top index of 5? It's driving me crazy. Any ideas?

Here's the code:

 
Mark Kevin
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never  mind. I found the answer in a ten-year-old post at this link:
https://coderanch.com/wiki/659883/Ocjp-Faq#kb-regexp

... which says that a String with 5 characters has indexes from 0 to 6. But how can that be? I was never taught this. Am I the only one who doesn't understand this?
 
Mark Kevin
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sorry. I haven't posted in a couple of years and forgot how to edit. The answer actually states a string with 6 characters has indexes of 0 to 6. Makes no sense to me.

 
Ranch Hand
Posts: 91
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

Hoping that this is more than a curiosity for you:

I once used a proprietary language (for telecom real time software) in which strings were coded
as lenght (the number of characters), followed by the characters themselves. The length is accessible
as an index too, so that leaves you with a string that has more indexes than characters...

I don't know if that's the way Java handles things...
 
Mark Kevin
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks, Mano. It's not a mere curiosity for me. I'm studying for the OCP 7 exam and this threw me. I thought the "hidden" characters at the beginning and end of String only applied to Regex \b and \B. It's still confusing, but I'll sort it out eventually. Strange concept.

Mark
 
Enthuware Software Support
Posts: 4291
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The pattern \\d* means a string length 0 or more, consisting only of integers. This pattern will match at every index of the input string because at every index there is a String of integers of at least 0 length, except at index 2, where it finds 34.
6 is indeed not a valid index in the given String (because its length is only 6, so indexing will be from 0 to 5), however, "beginning" and "end" are independent concepts in pattern matching that do not necessarily match with String index as this example shows. Last character of the input String is indeed at index 5 but from the perspective of the matcher, the string "ends" at position 6. You can think of it as a String terminator that exists just after the last character of the string.
The given pattern does match with the string terminator as well (because of the star) and so the matcher returns 6 as well.

Change the pattern from "\\d*" to "$" and you will see that it prints 6. Because $ matches "end of input" and 6 is where the input ends.

 
Mark Kevin
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Paul. Good answer. Can I take it then that there is an equivalent beginning of a String under these circumstances? Or is it only at the end?
 
Paul Anilprem
Enthuware Software Support
Posts: 4291
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark Kevin wrote:Thanks, Paul. Good answer. Can I take it then that there is an equivalent beginning of a String under these circumstances? Or is it only at the end?


Indeed, there is a beginning as well. It is denoted by the ^ character. But the position of the "beginning of input" coincides with the index of the first character of the string i.e. 0.
 
Popeye has his spinach. I have this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!