简体   繁体   English

为'whitespace`&`和`拆分字符串

[英]Splitting of string for `whitespace` & `and`

String filter = phoneNumber eq '763436' and  carrier eq 'AT and T Mobility' and site startswith '256'

the split should give me the following: 分裂应该给我以下内容:

String 1 = phoneNumber eq 763436 字符串1 = phoneNumber eq 763436

String 2 = carrier eq 'AT and T Mobility' 字符串2 =运营商eq'AT和T Mobility'

String 3 = site startswith 256 字符串3 =站点以256开头

Further more String 1 2 and 3 should split into 更多字符串1 2和3应分成

String operator = phoneNumer String operator = eq String operand =763436 String operator = phoneNumer String operator = eq String operand = 763436

String operator = carrier String operator = eq String operand =AT and T Mobility String operator = carrier String operator = eq String operand = AT and T Mobility

String operator = site String operator = startswith String operand =256 String operator = site String operator = startswith String operand = 256

I can use string.split("and") and split(" ") for outer and inner respectively, but my string too contains whitespace and and also (example AT and T Mobility). 我可以分别对外部和内部使用string.split(“和”)和split(“”),但我的字符串也包含空格,并且(例如AT和T Mobility)。 One help is that extra and and white space will be present in operator only within quotes. 一个帮助是,仅在引号内的运算符中将存在额外和空白空间。

Any Help how to split in java? 任何帮助如何在java中拆分?

Since split is regex-based, and if I'm reading the question correctly, you can just split for either one: 由于split是基于regex的,如果我正确地阅读了这个问题,你可以拆分为任何一个:

String[] split = yourString.split("(and)|\s+");

Edit : 编辑

Rather than splitting through regex for this, I would honestly recommend parsing the string yourself for something this specific: 我会诚实地建议你自己解析一下这个特定的东西,而不是通过正则表达式进行拆分。

public String[] parseRawString(String raw) {
    List<String> args = new ArrayList<>();
    StringBuilder sb = new StringBuilder();
    //whether or not to split on spaces
    boolean inQuotes = false;
    for (char c : raw.toCharArray()) {
        //if a quote is found
        if (c == '\'') {
            inQuotes = !inQuotes;
        //if a space is found outside quotes
        } else if (char == ' ' && !inQuotes) {
            args.add(sb.toString());
            sb.clear();
        //if a normal character is found or we're inside a quote
        } else if (char != ' ' || inQuotes) {
            sb.append(c);
        }
    }
    //add any last remnants that weren't added before the end
    if (!sb.isEmpty()) {
        args.add(sb.toString());
    }
    return args.toArray(new String[args.size()]);
}

This allows for some easier parsing: 这允许一些更容易的解析:

String[] one = parseRawString("phoneNumber eq 763436");
/*
    one[0] = phoneNumber
    one[1] = eq
    one[2] = 763436
*/
String[] two = parseRawString("carrier eq 'AT and T Mobility'");
/*
    two[0] = carrier
    two[1] = eq
    two[2] = AT and T Mobility
*/
//etc...

That seems a bit closer to what you want, I believe. 我相信,这似乎更接近你想要的东西。

Since you want those string as key,operand,value you can try this 既然你想要那些字符串作为键,操作数,值,你可以试试这个

    String regex="(?<key>\\S+)\\s+(?<operator>\\S+)\\s+(?<value>'[^']*'|\\S+)";
    for(String s:filter.split("\\s*and\\s*(?=([^']*'[^']*')*[^']*$)"))
    {
        Matcher m=Pattern.compile(regex).matcher(s);
        while(m.find())
        {
            System.out.println("Key:"+m.group("key"));
            System.out.println("operator:"+m.group("operator"));
            System.out.println("Value:"+m.group("value"));
        }
    }

Check Out This Answer. 看看这个答案。 It's working 它正在发挥作用

PROGRAM: 程序:

    String filter = "phoneNumber eq 763436 and  carrier eq 'AT and T Mobility' and site startswith 256";
    String[] split = filter.split("\\s*and\\s*(?=([^']*'[^']*')*[^']*$)");
    for (String s : split) {
       String[] split1 = s.trim().split(" ",3);
       for (String s1 : split1) {
            System.out.println(""+s1.replaceAll("'", ""));
        }
    }

OUTPUT: OUTPUT:

    phoneNumber
    eq
    763436
    carrier
    eq
    AT and T Mobility
    site
    startswith
    256

Check out the updated program as that one works for all types of input strings like below, 查看更新的程序,因为它适用于所有类型的输入字符串,如下所示,

1.String filter = "'phone Number' eq '763436' and carrier eq 'AT and T Mobility' and site 'startswith' '256'"; 1.String filter =“'电话号码'eq'763436'和载波eq'AT和T Mobility'和网站'startwith''256'”;

2.String filter = "'phone Number' eq '763436' and carrier eq 'AT and T Mobility' and site 'starts with' '256'"; 2.String filter =“'phone Number'eq'763436'和carrier eq'AT and T Mobility'和site'以''256'开头”;

3.String filter = "'phone and Number' eq '763436' and carrier eq 'AT and T Mobility' and site 'starts with' '256'"; 3.String filter =“'phone and Number'eq'763436'和carrier eq'AT and T Mobility'和site'以''256'开头”;

4.String filter = "phoneNumber eq '763436' and carrier eq 'AT and T Mobility' and site startswith '256'"; 4.String filter =“phoneNumber eq'763436'和载波eq'AT和T Mobility',站点以'256'开头”;

5.String filter = "phoneNumber eq 763436 and carrier eq 'AT and T Mobility' and site startswith 256"; 5.String filter =“phoneNumber eq 763436 and carrier eq'AT and T Mobility'and site startswith 256”;

even more you can as many strings like above with as single quotes.it's working for all of them.. 甚至更多你可以像上面一样多的字符串作为单引号。它适用于所有这些...

UPDATED PROGRAM 更新的计划

    String filter = "'phone Number' eq 763436 and  carrier eq 'AT and T Mobility' and site 'startswith' '256'";
    String[] split = filter.split("\\s*and\\s*(?=([^']*'[^']*')*[^']*$)");
    for (String s : split) {
        String[] split1 = s.trim().split("\\s* \\s*(?=([^']*'[^']*')*[^']*$)");
        for (String s1 : split1) {
            System.out.println("" + s1.replaceAll("'", ""));
        }
    }

OUTPUT: OUTPUT:

    phone Number
    eq
    763436
    carrier
    eq
    AT and T Mobility
    site
    startswith
    256

Check the updated code and update the status 检查更新的代码并更新状态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM