• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java regex output

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,

I am not understanding the output of this code:
I know that pattern selects either any 5 characters(excluding new line) followed by space OR it selects maximum 5 characters.
I am not getting why the second iteration of m.find() displays :


Output it gives:
Length:5group:Reden
Length:2group:: Length:5group:Infor
Length:6group:matie

Length:0group:
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output is exactly what I would expect. You have two groups but you select just group() so for each iteration you pick the total match from the end of the previous match however that match was achieved.

"Length:5group:Reden" is matched by "(.{0,5})"
"Length:2group:: " is matched by "(.{1,5}(?:\\s|$))"
"Length:5group:Infor" is matched by "(.{0,5})"
"Length:6group:matie" is matched by "(.{1,5}(?:\\s|$))"
"Length:0group:" is matched by "(.{0,5})"

Once the end-of-line is found the iterations cease.

P.S. Your printout is very poor. Next time please try to make it easier to understand.
P.P.S. I'm sure the regex could be re-written to make the intent clearer but since you don't say what it should do nobody here is going to be able to help improve it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic