• 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

"Empty" String v's String.length() = 0

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question uses a regex code example from S&B page 498 but is not about regex. S&B state that m.group() can return an "empty string" (page 508) so since (1) prints 0 why don't (2) and (3) give the same result ?
Execute the code >javac GBTestR1X1 "d\*" ab34ef
I have the feeling that it's somewhat obvious and should thank you for ypur replies with a blushing Graemlin (but there isn't one).
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, for group() to return an empty string, your regex would need to allow empty strings to match, or so it seems to me. As far as I can tell, your's doesn't.
 
Marshal
Posts: 74606
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And there always used to be a blushing graemlin. 3rd in the top row.
 
Graeme Byers
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well Sheriff ,
I think you should 1. read the question and 2. bone up on regex.

1. Why will (2) print a "?" and (1) does not ? Try execcuting the code.
2. Metacharacter * matches ZERO or more times.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Graeme Byers:
Well Sheriff ,
I think you should 1. read the question and 2. bone up on regex.



Not sure how I deserved such a harsh reaction. I was just trying to help, and to do that, I actually *did* read the question *and* tried to understand, although I did find it to be a bit ambiguous.

Also, I'm confused now, because your restatement of the question actually seems to ask something different than the original question. Might just be me being dumb, of course.

Anyway, you might want to look at what a backslash does to metacharacters in regular expressions.

Additionally, never compare Strings using "==", always use equals.
 
Rancher
Posts: 4062
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Graeme, such rudeness is not called for here. I would have answered your question much as Ilja did, and I still don't see how that does not help you. Ilja addressed a major reason why you're not seeing the same results from statements (2) and (3) as from (1): statements (2) and (3) never execute at all. Because find() returns false, because your regex never occurs anywhere inside the search string.

There are other reasons why these statements might give different behavior than what you're expecting, but I think that is the biggest one.

If that's not what you were trying to ask, I would suggest that try to politely rephrase your question.
 
Sheriff
Posts: 67618
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sure what Graeme Byers meant to say was "Gee thanks Ilja for trying to help me with my question. But I'm not sure I see how your answer helps me. Let me try to explain in more detail..."

Right?
 
Campbell Ritchie
Marshal
Posts: 74606
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Simmons:
Graeme, such rudeness is not called for here.



Thank you very much for stepping in, Mike.

You do not have a metacharacter which counts any number of occurrences. You have an asterisk, because you escaped it.
 
Graeme Byers
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "d\*" was a typo and should read "\d*".

A blushing gremlin (as I suggested this code might warrant , I really can't imagine what I was thinking) for the "==" String compare - that is what is actually causing the 'comparison failure' between an empty String and String length 0.

My sincere apologies to Ilja Preuss for my over harsh reaction to his attempt to assist with my problem.
[ October 28, 2008: Message edited by: Graeme Byers ]
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apologies accepted.
 
Campbell Ritchie
Marshal
Posts: 74606
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apologies accepted.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic