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
shakah - 14 Jun 2005 04:49 GMT
> I'm having trouble finding a regular expression to do what I want.
>
[quoted text clipped - 19 lines]
> 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'