Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""Empty" String v Watch ""Empty" String v New topic
Author

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

Graeme Byers
Ranch Hand

Joined: Apr 16, 2004
Posts: 127
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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
And there always used to be a blushing graemlin. 3rd in the top row.
Graeme Byers
Ranch Hand

Joined: Apr 16, 2004
Posts: 127
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

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Jan 10, 2002
Posts: 61764
    
  67

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Apr 16, 2004
Posts: 127
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

Joined: Jul 11, 2001
Posts: 14112
Apologies accepted.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Apologies accepted.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "Empty" String v's String.length() = 0