Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a quoted string with a delimiter

Tags:

java

string

split

I want to split a string with a delimiter white space. but it should handle quoted strings intelligently. E.g. for a string like

"John Smith" Ted Barry 

It should return three strings John Smith, Ted and Barry.

like image 945
fastcodejava Avatar asked May 22 '12 02:05

fastcodejava


1 Answers

After messing around with it, you can use Regex for this. Run the equivalent of "match all" on:

((?<=("))[\w ]*(?=("(\s|$))))|((?<!")\w+(?!"))

A Java Example:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Test
{ 
    public static void main(String[] args)
    {
        String someString = "\"Multiple quote test\" not in quotes \"inside quote\" \"A work in progress\"";
        Pattern p = Pattern.compile("((?<=(\"))[\\w ]*(?=(\"(\\s|$))))|((?<!\")\\w+(?!\"))");
        Matcher m = p.matcher(someString);

        while(m.find()) {
            System.out.println("'" + m.group() + "'");
        }
    }
}

Output:

'Multiple quote test'
'not'
'in'
'quotes'
'inside quote'
'A work in progress'

The regular expression breakdown with the example used above can be viewed here:

http://regex101.com/r/wM6yT9


With all that said, regular expressions should not be the go to solution for everything - I was just having fun. This example has a lot of edge cases such as the handling unicode characters, symbols, etc. You would be better off using a tried and true library for this sort of task. Take a look at the other answers before using this one.

like image 62
13 revs Avatar answered Sep 19 '22 15:09

13 revs