• Post Reply Bookmark Topic Watch Topic
  • New Topic

Different result using Regex example in SCJP6 book  RSS feed

 
M Davis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hey guys,

I did a search and could not find any information on this and it's sort of bugging me. I'm having trouble moving on because I feel like I am missing something.

It concerns the Regex extraction of file names beginning with "proj1" from the text string.

The string looks like this: ..."proj3.txt,proj1sched.pdf,proj1,proj2,proj1.java"... in the book.

The Regex expression in the book is; proj1([^,])*

The results are supposed to be:

10 proj1sched.pdf
25 proj1
37 proj1.java


For some reason when I try this all I get are the numbers without the actual file names. My results look like this: 10 25 37 without the file names.

Could somebody suggest what may be wrong here? I've read through a couple of times and I must be missing something. Could the book be incorrect here?

Thank you for any attention you can give this.

Mark
 
M Davis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here's the short mini-program that I am using to test this...

 
Prashant Shiralkar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably, you missed it: start() only gives you the starting position of a match. To actually have the matching string, you'd need to use group() method of Matcher class
 
M Davis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I did miss that. Thank you so much for your response. I appreciate it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!