File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "regex (A Strange Problem)" Watch "regex (A Strange Problem)" New topic

regex (A Strange Problem)

Dante Sparda

Joined: Jan 12, 2013
Posts: 19

Hi Friends,

Please pay attention to the following code:


Now I change the str to "aaa111222333":


I can't understand the reason of different result of above codes!!!

OCPJP 6 (80%)
John Jai

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

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:
subject: regex (A Strange Problem)
It's not a secret anymore!