Results 1 to 6 of 6

Thread: Pattern used with split

  1. #1
    Join Date
    Dec 2009
    Posts
    213

    Pattern used with split

    Hello,
    I wonder if it is possible to maintain the pattern used with the split method to split a sentence.
    For example with
    Code:
    String sen = "The little cat."
    String[] phr = sen.split("small");
    / / phr [0] contains "The" and phr [1] contains "cat."
    Is it possible to have some work such phr [0] = "The" then phr[1] = "small" (pattern cutting) and phr [2] = "cat". Any help on this will be highly appreciated. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    446

    Re: Pattern used with split

    Hello,
    Yes it is possible, but using regular expressions instead of a split:
    Code:
    String sentence = "kitten";
    Pattern pat = Pattern.compiles("^(.+)( small )(.+)$");
    Matcher mat = pat.matcher(sentence);
     
    if(mat.games()) {
       System.out.System.out.println(mat.group(1)); / / "My"
       System.out.System.out.println(mat.group(2)); / / "Small"
       System.out.System.out.println(mat.group(3)); / / Chat
    }
    Try this and see if the code works perfect for you.

  3. #3
    Join Date
    Nov 2009
    Posts
    356

    Re: Pattern used with split

    Hello,
    I think instead of the split with "small", you do it with one or more spaces:
    Here is the code for it
    Code:
    String[] phr = sentence.split(" +");
    I do not know what you exactly want the output of your code, but if you need this as the output then it will be a great option for your program. Try it once and if not successful, then leave it and try some other option.

  4. #4
    Join Date
    Nov 2009
    Posts
    347

    Re: Pattern used with split

    Hello,
    In my opinion no, no such base. It remains you to retrace the table later to create a second table with your separators (here "small") between your words. Split with the "+" does not answer the question in my mind as to the specific example given. The regular expression not because it would require to know in advance how much of a "small" in the chain. Hope you are understanding the circumstances of it.

  5. #5
    Join Date
    Nov 2009
    Posts
    330

    Re: Pattern used with split

    Hello,
    You can try the following code
    Code:
    Public static String[] split(String in, String reg, boolean incsep) {
    		/ / We create a matcher that will search all partitions:
    		Matcher m = Pattern.compiles(reg).matcher(in);
     
    		/ / List of 'words' to return:
    		List <String> mtlst = new ArrayList <String>();
    		/ / Index out of words to be cut:
    		int index = 0;
     
    		/ / It searches for each separator:
    		while(m.find()) {
    			/ / We cut the string before the separator found:
    			mtlst.add( in.substring(index, m.home()) );
    			
    			if (incsep) {
    				/ / It returns the separator:
    				mtlst.add( m.group() );
    			}
    			
    			/ / We move the index after the separator
    			index = m.end();
    		}
    		
    		if (Index ==0) {
    			/ / Special case: no separator found
    			/ / It returns the entire string:
    			return new String[] {in};
    		}
    		
    		if (index <in.length()) {
    			/ / We add the end of the chain (last word)
    			mtlst.add( in.substring(Index) );
    		}
    		return mtlst.toArray(new String[mtlst.size()]);
    	}

  6. #6
    Join Date
    Nov 2009
    Posts
    333

    Re: Pattern used with split

    Hell,
    Eclipse, when you have this error message, simply go left into the small cross of the error. Among the solutions to resolve the error, he offers hopefully "Change source level to ". This is the solution to be learned. Otherwise, the alternative is to make the code compatible java 1.4 (which is simply to remove "<String>). If you need more information on Eclipse the you can visit their official site.

Similar Threads

  1. EA Sims 3 Pattern Tool
    By The$Hulk in forum Video Games
    Replies: 7
    Last Post: 22-10-2010, 03:48 AM
  2. Replies: 7
    Last Post: 03-08-2010, 11:42 AM
  3. Problem in the split pattern
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 18-02-2010, 05:55 AM
  4. How to use for loop to get particular pattern in c++?
    By Juaquine in forum Software Development
    Replies: 4
    Last Post: 29-01-2010, 05:19 PM
  5. Replies: 1
    Last Post: 25-04-2009, 05:17 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,749,963,964.90819 seconds with 16 queries