String.split regular expression - need help

Discussion in 'Java' started by Richard, Oct 3, 2005.

  1. Richard

    Richard Guest

    Hello:

    Given a
    String s = "{abc}{def}{X}{Y}";

    I would like to split it into
    part[0] = "abc";
    part[1] = "abc";
    part[2] = "abc";
    part[3] = "abc";

    Attempted
    String[] part = s.split("^\\{|\\}\\{|\\}$");
    but that gives an array with "" as a first element

    Is there a better regex to extract each item inside {} in my original
    string?

    Thanks,
    Richard
     
    Richard, Oct 3, 2005
    #1
    1. Advertising

  2. Richard

    Guest

    > part[0] = "abc";
    > part[1] = "abc";
    > part[2] = "abc";
    > part[3] = "abc";

    I don't understand your requirement.
     
    , Oct 3, 2005
    #2
    1. Advertising

  3. wrote:

    >>part[0] = "abc";
    >>part[1] = "abc";
    >>part[2] = "abc";
    >>part[3] = "abc";

    >
    > I don't understand your requirement.


    I think that should have read..

    part[0] = "abc";
    part[1] = "def";
    part[2] = "X";
    part[3] = "Y";

    Perhaps the OP took 'always copy/paste' a little too
    literally. ;-)
     
    Andrew Thompson, Oct 3, 2005
    #3
  4. Richard

    Roedy Green Guest

    On 3 Oct 2005 10:18:52 -0700, "Richard" <> wrote
    or quoted :

    > String[] part = s.split("^\\{|\\}\\{|\\}$");
    >but that gives an array with "" as a first element


    That is exactly what is should do because there is a delimiter on the
    front. If your delimiter were / and you wrote

    "/abc/def/"

    you would expect "" abc def ""

    if you wrote

    "abc/def"

    you would expect abc def

    I think your expectations are being warped by the curliness of your
    delimiters.

    So the way to handle it is when you are done strip the lead and trail
    "", or strip all "".

    Then you can dispense with the special case ^ and $.
    You could even shorten your regex to

    "\\{|\\}"
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 3, 2005
    #4
  5. Richard

    shakah Guest

    Richard wrote:
    > Hello:
    >
    > Given a
    > String s = "{abc}{def}{X}{Y}";
    >
    > I would like to split it into
    > part[0] = "abc";
    > part[1] = "abc";
    > part[2] = "abc";
    > part[3] = "abc";
    >
    > Attempted
    > String[] part = s.split("^\\{|\\}\\{|\\}$");
    > but that gives an array with "" as a first element
    >
    > Is there a better regex to extract each item inside {} in my original
    > string?
    >
    > Thanks,
    > Richard


    Not a one-liner, but you could use groups, e.g.:

    jc@soyuz:~/tmp$ cat grptest.java
    public class grptest {
    public static void main(String [] asArgs) {
    java.util.regex.Pattern p =
    java.util.regex.Pattern.compile(asArgs[0]) ;
    System.out.println(" regex: [" + asArgs[0] + "]") ;
    for(int i=1; i<asArgs.length; ++i) {
    String sExpr = asArgs ;
    System.out.println("input str: [" + sExpr + "]") ;
    java.util.regex.Matcher m = p.matcher(sExpr) ;
    while(m.find()) {
    System.out.println(" match: [" + m.group(1) + "]") ;
    }
    }
    }
    }

    jc@soyuz:~/tmp$ java grptest '\{([^}]*)\}' "{abc}{def}{X}{Y}"
    regex: [\{([^}]*)\}]
    input str: [{abc}{def}{X}{Y}]
    match: [abc]
    match: [def]
    match: [X]
    match: [Y]
     
    shakah, Oct 3, 2005
    #5
  6. Richard

    Guest

    Then,

    String s = "{abc}{def}{X}{Y}";
    String[] p = s.split("\\{|\\}\\{|\\}");
    for (String ss : p){
    System.out.println(ss);
    }
     
    , Oct 4, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PC
    Replies:
    3
    Views:
    2,654
    Alex I. Varyanick
    Apr 24, 2005
  2. VSK
    Replies:
    2
    Views:
    2,300
  3. rhaavik
    Replies:
    6
    Views:
    4,757
    Hendrik Maryns
    Nov 17, 2005
  4. Sam Kong
    Replies:
    5
    Views:
    244
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    613
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page