Splitting a String on space with possible Quotes

R

rcurts

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
 
S

shakah

rcurts said:
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'
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top