Split a String based on regular expression

Posted on 2007-07-24
Last Modified: 2010-04-01
Hi All,

I have a simple query.....

 * @author RamanaReddy Suda
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Splitter {
    private int maxLen;
    private int maxCount;
    private int minCount;

    private static int defaultMaxLen = 32*1024;
    private static int defaultMaxCount = 12;
    private static int defaultMinCount = 2;

    // default constructor
    public Splitter() {
        maxCount = defaultMaxCount;
        minCount = defaultMinCount;
          maxLen = defaultMaxLen;

//  custom constructor: make new Splitter with specified maxLen value
    public Splitter(int maxLen) {
        this.maxLen = maxLen;
    // Constructor: make new Splitter with specified max and min values
    public Splitter(int maxCount, int minCount) {
             this.maxCount = maxCount;
        this.minCount = minCount;

    // accessor method: get the current maxLen value
    public int getMaxLen() {
        return maxLen;

    // mutator method: set maxLen to specified value
    public void setMaxLen(int maxLen) {
        this.maxLen = maxLen;

    // the real work gets done here:
    //   -- split the given string into substrings no longer than maxLen
    //   -- return as an array of strings
    public String[] split(String str) {
             int origLen = str.length();      
          // calculate the number of substrings we'll need to make
        int splitNum = origLen/maxLen;
        if (origLen % maxLen > 0)
            splitNum += 1;

        // initialize the result array
        String[] splits = new String[splitNum];

        // for each substring...
        for (int i = 0; i < splitNum; i++) {

            // the substring starts here
            int startPos = i * maxLen;

            // the substring ends here
            int endPos = startPos + maxLen;

            // make sure we don't cause an IndexOutOfBoundsException
            if (endPos > origLen)
                endPos = origLen;

            // make the substring
            String substr = str.substring(startPos, endPos);

            // stick it in the result array
            splits[i] = substr;

        // return the result array
        return splits;
    public String split(String str, int count) {
             //int origLen = str.length();      
          int splitNum=0;
          if (count > maxCount){
                splitNum = count/maxCount;
          if (count % maxCount > 0)
                splitNum += 1;
          System.out.println("split num inside split method " + splitNum);       
          return "test";
    public static void main(String[] argv) {
        int splitLen = 5;
        int length,index = 0;
        int count = 0;
        BufferedReader in = new BufferedReader(new InputStreamReader(;
        String str = "";
       // System.out.println("split length = "+splitLen);
        System.out.print("Enter a string: ");
        try {
                  str = in.readLine();
                  String patternStr = "<br> <br>";
                  Pattern pattern = Pattern.compile(patternStr);
                  Matcher matcher = pattern.matcher(str);
                     index = str.length();
                     count = count+1;
                     System.out.println("Count in main method is "+ count + " index" + index);
                     String result = new Splitter().split(str,count);
                     System.out.println(" result is "+result);
        } catch (IOException err) {
            System.out.println("IOException: "+err);
I want split the sting based on the regular expression.....  I will do some other calculations later.. But all I need is what could be the best way to cut the string based on that expression.

I'm trying to identify the String index value and will pass that string index to the split method..

Based on the above code can any one help me to find the index number (or) any other way to split it...

Question by:Suda_RamanaReddy
    LVL 9

    Expert Comment

    What are you trying to do ?

    Can you give me an example of input, and the result of it ?

    Thank you.

    Author Comment

    I am getting a string as a input and that string may consists of few patterns in between the actual text... (it could be <br> <br>  (or) </p>....)When ever I encountered that pattern I have to split the string into a paragraph.
    once I have 4 such paragraphs I have to keep it in a page. next 4 paragraphs in second page.. etc...!
    As of now I'm reading the string and able to identify the pattern...and when I find the pattern I'm invoking a split method which will be doing the necessary splitting for me.
    My logic could be wrong... but as of now I'm trying identify the string length when ever the pattern match found and trying to insert that paragraph into an array. But I didn;t find any matching method to do that.

    Hope you got the problem now.....  All I need is I want to find the index (or) length of the string when matches found. Then I guess I can do the remaining thing.
    LVL 9

    Accepted Solution

    Excuse me if I am wrong,
    but you could just split the input ?

          String[] result = str.split("<br>");

          for (int x=0; x<result.length; x++)  System.out.println(result[x] + ", length: " + result[x].length());

    Author Comment

    Thanks Mathew
    LVL 9

    Expert Comment

    No problem,
    in the future ask me further information/comments if that is insufficient so that I can have the grade A, it's better!

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now