Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regex (A Strange Problem)

 
Dante Sparda
Greenhorn
Posts: 19
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Friends,

Please pay attention to the following code:


Result:


Now I change the str to "aaa111222333":



Result:


I can't understand the reason of different result of above codes!!!
 
John Jai
Rancher
Posts: 1776
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The regex expression provided - "\\d{3}" denotes the split has to happen for every 3 numbers. Note that trailing empty strings are not returned as part of the split tokens hence you get only one String in the array. Change the input as aaa111222333a and you will see the empty strings returned.
 
Alexandre Leveille
Greenhorn
Posts: 12
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dante,

To complete John's answer, have a look at the following code (inspired from Java 6 API):





Another example in-between the last one and your case is:




So as you can see, the regex in the split is used as the delimiter to split your string. Everything in-between this delimiter will be returned in the array, but the delimiter itself is excluded.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic