Splitting a String on space with possible Quotes

Discussion in 'Java' started by rcurts, Jun 14, 2005.

  1. rcurts

    rcurts Guest

    I'm having trouble finding a regular expression to do what I want.

    I get a string that I want to split by spaces, and the tokens may or
    may not be encapsulated by quotes.

    I would like to take:

    DOG CAT "LAUGHING HYENA" BIRD
    and get

    0: DOG
    1: CAT
    2: LAUGHING HYENA
    3: BIRD

    I won't know if a token will come through encapsulated by quotes or
    not. I was thinking of looping through each char in the string, but I
    can only hope there's a better way to do this.

    Any ideas?

    Thanks
    rcurts
    rcurts, Jun 14, 2005
    #1
    1. Advertising

  2. rcurts

    shakah Guest

    rcurts wrote:
    > I'm having trouble finding a regular expression to do what I want.
    >
    > I get a string that I want to split by spaces, and the tokens may or
    > may not be encapsulated by quotes.
    >
    > I would like to take:
    >
    > DOG CAT "LAUGHING HYENA" BIRD
    > and get
    >
    > 0: DOG
    > 1: CAT
    > 2: LAUGHING HYENA
    > 3: BIRD
    >
    > I won't know if a token will come through encapsulated by quotes or
    > not. I was thinking of looping through each char in the string, but I
    > can only hope there's a better way to do this.
    >
    > Any ideas?
    >
    > Thanks
    > rcurts


    There's got to be a better way, but how about
    something like '"([^"]*)"| ?([^ ]+) ?' ?

    jc@sarah:~/tmp$ cat regextest3.java
    public class regextest3 {
    public static void main(String [] asArgs) {
    java.util.regex.Pattern p =
    java.util.regex.Pattern.compile(asArgs[0]) ;
    for(int nArg=1; nArg<asArgs.length; ++nArg) {
    System.out.println("looking in '" + asArgs[nArg] + "'") ;
    java.util.regex.Matcher m = p.matcher(asArgs[nArg]) ;
    while(m.find()) {
    for(int i=0; i<m.groupCount(); ++i) {
    String sTemp = m.group(i+1) ;
    if(null!=sTemp) {
    System.out.println(" found: '" + sTemp + "'") ;
    }
    }
    }
    }
    }
    }

    jc@sarah:~/tmp$ /usr/java/jdk1.5.0_01/bin/java regextest3 '"([^"]*)"|
    ?([^ ]+) ?' 'DOG "" CAT "LAUGHING HYENA" BIRD'
    looking in 'DOG "" CAT "LAUGHING HYENA" BIRD'
    found: 'DOG'
    found: ''
    found: 'CAT'
    found: 'LAUGHING HYENA'
    found: 'BIRD'
    shakah, Jun 14, 2005
    #2
    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. Chris
    Replies:
    1
    Views:
    13,609
    Oisin
    Mar 24, 2006
  2. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,869
    Bryan Bullard
    Jul 11, 2003
  3. Christian Seberino
    Replies:
    21
    Views:
    1,629
    Stephen Horne
    Oct 27, 2003
  4. Ian Bicking
    Replies:
    2
    Views:
    984
    Steve Lamb
    Oct 23, 2003
  5. Ian Bicking
    Replies:
    2
    Views:
    709
    Michael Hudson
    Oct 24, 2003
Loading...

Share This Page