Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problems with regex

 
susana miranda
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tag to post the code like


 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
* means 0 or more

So it will consider each element of line as eligible.
 
susana miranda
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, as you know * means 0 or more and after 90 there is nothing so it will be counted.
 
susana miranda
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic