This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding regex 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 "Regarding regex" Watch "Regarding regex" New topic

Regarding regex

B pandoo
Ranch Hand

Joined: Nov 14, 2006
Posts: 37
public class Sting
public static void main(String[] args){
String str = "aaaaaaaaabb";
String[] s = str.split("a{3}");

Answer is 4.
pleas explain the output..
what does a{3} represents?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Look at the API for java.util.regex - the Pattern class and under Greedy Quantifiers.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Abdul Rehman
Ranch Hand

Joined: Nov 07, 2006
Posts: 65
The regex "a{3}" means the character 'a' exactly 3 times.
To understand the output, you should know how the split() method works. See the API for this method in the java.util.regex.Pattern class.
After that, try playing with the code. Here is a modified version of the code alongwith the output.

This gives the output:


Think out why we get such an output? Once done, look at your own code. The only change is that the 'x' characters have been omitted in your version. So, think out why the output is 4?

Abdul Rehman.

SCJP 5.0 (100%)
Have you checked out Aspose?
subject: Regarding regex
It's not a secret anymore!