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

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

问题描述

我想用分隔符空格分割一个字符串.但它应该智能地处理带引号的字符串.例如.对于像

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.

推荐答案

搞定之后,你可以使用 Regex 来解决这个问题.运行相当于全部匹配"的代码:

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天全站免登陆