File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes regex (A Strange Problem) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "regex (A Strange Problem)" Watch "regex (A Strange Problem)" New topic
Author

regex (A Strange Problem)

Dante Sparda
Greenhorn

Joined: Jan 12, 2013
Posts: 19


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!!!


OCPJP 6 (80%)
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Jan 26, 2013
Posts: 12

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regex (A Strange Problem)
 
Similar Threads
Doubt in split()
String.split()
Zero length matches??
Regarding regex
Question about String split() ... (was DOUBT)