You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oro-user@jakarta.apache.org by "Sharpe, Cassandra" <ca...@merck.com> on 2002/05/31 14:24:32 UTC

Using the Util.split() method

I am trying to split pipe delimited text using the Util.split() method.
However, instead of splitting the string based upon the pattern, it is
splitting the string along every character.  I included a code example
below.  When I split the pipe delimited string, I get 39 results.  When I
split the comma delimited string, I get 7 results, which is what I want.
Does anyone have any suggestions?

Thank You,
Cassandra


 import org.apache.oro.text.regex.*;
 import java.util.ArrayList;

public class TestSplit {


  private static String pipes = "E|1|1|abdomen|abdomen|abdomin|abdomin";
  private static String commas = "E,1,1,abdomen,abdomen,abdomin,abdomin";

  public TestSplit() {}

  public static void main( String[] args )throws Exception {

    ArrayList results = new ArrayList();

    Pattern commaPattern = null;
    Pattern pipePattern = null;

    PatternMatcher matcher = new Perl5Matcher();
    PatternCompiler compiler = new Perl5Compiler();


    pipePattern = compiler.compile("|");

    commaPattern = compiler.compile(",");

      Util.split(results, matcher, commaPattern, commas,  Util.SPLIT_ALL);
      System.out.println("Number with commas: " + results.size());

      results.clear();

      Util.split(results, matcher, pipePattern, pipes,  Util.SPLIT_ALL);
      System.out.println("Number with pipes: " + results.size());
  }
}



------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (Whitehouse Station, New Jersey, USA) that may be confidential, proprietary copyrighted and/or legally privileged, and is intended solely for the use of the individual or entity named in this message.  If you are not the intended recipient, and have received this message in error, please immediately return this by e-mail and then delete it.

==============================================================================


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Using the Util.split() method

Posted by Ed Chidester <ec...@textwise.com>.

Cassandra,

The pipe is a special regular expression character. If you escape the pipe
(as I did in the attached file) you'll get the results that you expect.


   % java TestSplit
   Number with commas: 7
   Number with pipes: 39
   Number with pipes (escaped) : 7


Cheers,

Ed.

// Edward Chidester
// Software Engineer                            MNIS - Textwise Labs
// (315) 426-9311 x232                          401 South Salina Street
// echid@textwise.com                           Syracuse, NY 13202

On Fri, 31 May 2002, Sharpe, Cassandra wrote:

> 
> I am trying to split pipe delimited text using the Util.split() method.
> However, instead of splitting the string based upon the pattern, it is
> splitting the string along every character.  I included a code example
> below.  When I split the pipe delimited string, I get 39 results.  When I
> split the comma delimited string, I get 7 results, which is what I want.
> Does anyone have any suggestions?
> 
> Thank You,
> Cassandra
> 
> 
>  import org.apache.oro.text.regex.*;
>  import java.util.ArrayList;
> 
> public class TestSplit {
> 
> 
>   private static String pipes = "E|1|1|abdomen|abdomen|abdomin|abdomin";
>   private static String commas = "E,1,1,abdomen,abdomen,abdomin,abdomin";
> 
>   public TestSplit() {}
> 
>   public static void main( String[] args )throws Exception {
> 
>     ArrayList results = new ArrayList();
> 
>     Pattern commaPattern = null;
>     Pattern pipePattern = null;
> 
>     PatternMatcher matcher = new Perl5Matcher();
>     PatternCompiler compiler = new Perl5Compiler();
> 
> 
>     pipePattern = compiler.compile("|");
> 
>     commaPattern = compiler.compile(",");
> 
>       Util.split(results, matcher, commaPattern, commas,  Util.SPLIT_ALL);
>       System.out.println("Number with commas: " + results.size());
> 
>       results.clear();
> 
>       Util.split(results, matcher, pipePattern, pipes,  Util.SPLIT_ALL);
>       System.out.println("Number with pipes: " + results.size());
>   }
> }
> 
> 
> 
> ------------------------------------------------------------------------------
> Notice:  This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (Whitehouse Station, New Jersey, USA) that may be confidential, proprietary copyrighted and/or legally privileged, and is intended solely for the use of the individual or entity named in this message.  If you are not the intended recipient, and have received this message in error, please immediately return this by e-mail and then delete it.
> 
> ==============================================================================
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 
>