使用分隔符拆分带引号的字符串 [英] Split a quoted string with a delimiter

查看:538
本文介绍了使用分隔符拆分带引号的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拆分带分隔符空格的字符串。但它应该智能地处理引用的字符串。例如。对于像

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 

它应该返回三个字符串John Smith,Ted和Barry。

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

推荐答案

搞乱之后,你可以使用正则表达式。运行相当于全部匹配:

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

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

Java示例:

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() + "'");
        }
    }
}

输出:

'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

尽管如此,正则表达式不应该是解决方案为了一切 - 我只是玩得开心。这个例子有很多边缘情况,比如处理unicode字符,符号等。你最好使用一个久经考验的库来完成这类任务。在使用这个答案之前,请先看看其他答案。

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.

这篇关于使用分隔符拆分带引号的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆