wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes problems with regex Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "problems with regex" Watch "problems with regex" New topic
Author

problems with regex

susana miranda
Greenhorn

Joined: May 21, 2008
Posts: 9
Hi everybody! this is my first post
and... I have this class



I understand the result of my first execution:

% /SCJP/exercises$ java Regex
Pattern: \d+
Source: 123a456b78ccc0
start: 0 ---group: 123
start: 4 ---group: 456
start: 8 ---group: 78
start: 13 ---group: 0

but not the second

% /SCJP/exercises$ java Regex
Pattern: \d*
Source: 123a456b78ccc90
start: 0 ---group: 123
start: 3 ---group:
start: 4 ---group: 456
start: 7 ---group:
start: 8 ---group: 78
start: 10 ---group:
start: 11 ---group:
start: 12 ---group:
start: 13 ---group: 90
start: 15 ---group:

Why with \d* it returns an empty group if with \d i mean only numbers. I am a little confused :s

Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

Please use code tag to post the code like


Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

* means 0 or more

So it will consider each element of line as eligible.
susana miranda
Greenhorn

Joined: May 21, 2008
Posts: 9
Ok Harpreet. Thanks a lot. I had misunderstood * Only one more question:
Why on the second execution returns position start: 15? If my source ends with 90. Is it because null counts or so?

% /SCJP/exercises$ java Regex
Pattern: \d*
Source: 123a456b78ccc90
start: 0 ---group: 123
start: 3 ---group:
start: 4 ---group: 456
start: 7 ---group:
start: 8 ---group: 78
start: 10 ---group:
start: 11 ---group:
start: 12 ---group:
start: 13 ---group: 90
start: 15 ---group:

Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

Yes, as you know * means 0 or more and after 90 there is nothing so it will be counted.
susana miranda
Greenhorn

Joined: May 21, 2008
Posts: 9
Ok, thanks a lot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problems with regex
 
Similar Threads
Generics SCJP 1.5
SCJP 6 K&B book page no: 499
Regex help
Doubt in Regular Expression
doubt on group() in Matcher class