Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Graeme Byers
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • 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).
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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.
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies accepted.
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies accepted.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic