simple Matcher question

M

Marc E

All, given this code:

Pattern p = Pattern.compile("\\d*");
Matcher m = p.matcher("a");
while(m.find()){
System.out.println("start is " + m.start() );
}

Why is the output
0
1

?

I understand that start returns the index of the start of the search, but
given that this string is only one character, I don't understand how it gets
to an index of 1. does the start value of 1 indicate that it's actually
searching after the "a"?, i.e. that somehow find() is returning true after
it hits the end of the string?

Thanks for a clear explanation
 
J

Jim Korman

All, given this code:

Pattern p = Pattern.compile("\\d*");
Matcher m = p.matcher("a");
while(m.find()){
System.out.println("start is " + m.start() );
}

Why is the output
0
1

?

I understand that start returns the index of the start of the search, but
given that this string is only one character, I don't understand how it gets
to an index of 1. does the start value of 1 indicate that it's actually
searching after the "a"?, i.e. that somehow find() is returning true after
it hits the end of the string?

Thanks for a clear explanation
You're searching for <zero or more> occurances of a digit.

Matching "a" at position 0

then {I'm assuming} matching nothing at the end of the string.

Then no more data and exit.

Jim
 
C

Chris Smith

Marc E said:
All, given this code:

Pattern p = Pattern.compile("\\d*");
Matcher m = p.matcher("a");
while(m.find()){
System.out.println("start is " + m.start() );
}

Why is the output
0
1

?

There are zero or more digits before the 'a', and there are zero or more
digits after the 'a'. If you had more characters, it would match
between each character, as well. When a regular expression matches an
empty substring at the end of a string, it returns the length of the
string. There's really no other option.

Most of the time, it's a mistake to use Matcher.find with a regular
expression that generates the empty string.
 
M

Marc E

cool. easy enough to digest. thanks guys.

Chris Smith said:
There are zero or more digits before the 'a', and there are zero or more
digits after the 'a'. If you had more characters, it would match
between each character, as well. When a regular expression matches an
empty substring at the end of a string, it returns the length of the
string. There's really no other option.

Most of the time, it's a mistake to use Matcher.find with a regular
expression that generates the empty string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top