Split a String based on regular expression

Hi All,

I have a simple query.....

 * @author RamanaReddy Suda
 */
import java.io.*;
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(System.in));
        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);
                                              
                  while(matcher.find()){
                     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);
            System.exit(1);
        }
        System.out.println(">>"+str+"<<");  
       
       
    }
}
------------------------------------------------------------------------------------------------------------
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...

Thanks
Suda_RamanaReddyAsked:
Who is Participating?
 
matthew016Connect With a Mentor Commented:
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());
0
 
matthew016Commented:
What are you trying to do ?

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

Thank you.
0
 
Suda_RamanaReddyAuthor Commented:
Matthew,
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.
0
 
Suda_RamanaReddyAuthor Commented:
Thanks Mathew
0
 
matthew016Commented:
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!
0
All Courses

From novice to tech pro — start learning today.