How to split a string with regular expression

Discussion in 'Java' started by rhaavik, Nov 16, 2005.

  1. rhaavik

    rhaavik Guest

    Hi.
    I need to split a string with a regular expression.
    Example
    String = "this is; a test";rune haavik;12345;
    And I want the output to be:
    "this is; a test"
    rune haavik
    12345

    If I use this code:
    private void test1()
    {
    String str = "\"this is; a test\";rune haavik;12345;";
    int i=0;
    String[] tmp = str.split(";");
    while(i<tmp.length)
    {
    System.out.println(tmp);
    i++;
    }
    }
    Then it splits also in the "" text.

    Regards
    Rune haavik
     
    rhaavik, Nov 16, 2005
    #1
    1. Advertising

  2. rhaavik

    Guest

    Could you elaborate?

    Then it splits also in the "" text.
     
    , Nov 16, 2005
    #2
    1. Advertising

  3. rhaavik

    Oliver Wong Guest

    "rhaavik" <> wrote in message
    news:...
    > Hi.
    > I need to split a string with a regular expression.
    > Example
    > String = "this is; a test";rune haavik;12345;
    > And I want the output to be:
    > "this is; a test"
    > rune haavik
    > 12345
    >
    > If I use this code:
    > private void test1()
    > {
    > String str = "\"this is; a test\";rune haavik;12345;";
    > int i=0;
    > String[] tmp = str.split(";");
    > while(i<tmp.length)
    > {
    > System.out.println(tmp);
    > i++;
    > }
    > }
    > Then it splits also in the "" text.


    For this particular task, I recommend against using regular expressions.
    I think it'll be much simpler if you do a character by character scan of the
    string, looking for ';', and toggling a flag every time you see '"'.

    - Oliver
     
    Oliver Wong, Nov 16, 2005
    #3
  4. rhaavik

    Roedy Green Guest

    On 16 Nov 2005 08:15:07 -0800, "rhaavik" <> wrote,
    quoted or indirectly quoted someone who said :

    >String = "this is; a test";rune haavik;12345;
    >And I want the ou


    you don't want ; inside " " to be treated as ;

    What you are doing is reading a CSV file. It will be much easier with
    a tool specialised for the job rather than attempting to write a tool
    with a generic regex.


    see http://mindprod.com/jgloss/csv.html

    CSVReader can be configured to use ; instead of ,. I take it you are
    In Sweden where MS products use ; instead of , in CSV files.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 16, 2005
    #4
  5. rhaavik

    rhaavik Guest

    Hi.
    I know I'm reading a csv file. And it is a dump from SAP.
    Because of other things I need to do with the contents in the file, I'm
    looking for a regex to do the job.
    Can anyone help with that?

    Thanx
    Rune
     
    rhaavik, Nov 17, 2005
    #5
  6. rhaavik

    Roedy Green Guest

    On 16 Nov 2005 22:45:15 -0800, "rhaavik" <> wrote,
    quoted or indirectly quoted someone who said :

    >I know I'm reading a csv file. And it is a dump from SAP.
    >Because of other things I need to do with the contents in the file, I'm
    >looking for a regex to do the job.
    >Can anyone help with that?


    Let's say someone kludges the ; for you. You will in a day or two come
    back and want the embedded quotes handled properly. You are on the
    thin fringes of what you can do with regex.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 17, 2005
    #6
  7. rhaavik schreef:
    > Hi.
    > I know I'm reading a csv file. And it is a dump from SAP.
    > Because of other things I need to do with the contents in the file, I'm
    > looking for a regex to do the job.
    > Can anyone help with that?


    Well, if you really need to, you might get more help on
    comp.lang.perl.misc, but there they will also forward you to some
    library which does balanced parens and stuff.

    H.

    --
    Hendrik Maryns

    ==================
    www.lieverleven.be
    http://aouw.org
     
    Hendrik Maryns, Nov 17, 2005
    #7
    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,668
    Alex I. Varyanick
    Apr 24, 2005
  2. VSK
    Replies:
    2
    Views:
    2,388
  3. Richard
    Replies:
    5
    Views:
    549
  4. Sam Kong
    Replies:
    5
    Views:
    276
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    707
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page