基于java中的小括号的分组拆分一个复杂的String [英] Split a complex String based on grouping of small brackets in java

查看:774
本文介绍了基于java中的小括号的分组拆分一个复杂的String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 (region =asia)AND((status = null)OR((inactive =true)AND(department =aaaa))OR((costcenter =ggg)OR(location =india))

我需要将其拆分并在我的代码中使用它,但是我必须考虑大括号,以便分组完全如图所示。分裂后,我必须在每次迭代中得到如下所示的内容,并将其分解下来



第一次:

$ b $ AND

((status = null)OR((inactive =true)AND( department =aaaa))OR((costcenter =ggg)OR(location =india)))

第二次:

 (region =asia)AND 



(status = null)OR

((inactive =true)AND(department =aaaa))OR
((costcenter =ggg)OR(location =india))


等等...



有关如何实现这一点的任何指针?

解决方案

似乎你不愿意进行全面的解析,正则表达式无法解决这种问题,也许是一个逐步的解决方案。



这里列出了变量,其中 i th 条目内部文本值为(...),其变量为 @ 123 ,其中 123 i

  static String parse(String exp,List< String> vars){
final Pattern BRACED_REDEX = Pattern.compile(\\(([^()] *)\\));
for(;;){
Matcher m = BRACED_REDEX.matcher(exp);
if(!m.find()){
break;
}
String value = m.group(1);
String var =@+ vars.size();
vars.add(value);
StringBuffer sb = new StringBuffer();
m.appendReplacement(sb,var);
m.appendTail(sb);
exp = sb.toString();
}
vars.add(exp); //添加上一个未导出的expr。
return exp;
}

public static void main(String [] args){
String exp =(region = \asia\)AND((status = null) OR((inactive = \true\)
+AND(department = \aaaa\))OR((costcenter = \ggg\)OR
+(location = \india\)));
列表< String> vars = new ArrayList<>();
exp = parse(exp,vars);
System.out.println(Root expression:+ exp); (int i = 0; i< vars.size(); ++ i){
System.out.printf(@%d =%s%n,i,vars。得到(I));
}
}

这将给

 根表达式:@ 0 AND @ 8 
@ 0 = region =asia
@ 1 = status = null
@ 2 = inactive =true
@ 3 = department =aaaa
@ 4 = @ 2 AND @ 3
@ 5 = costcenter =ggg
@ 6 = location =india
@ 7 = @ 5 OR @ 6
@ 8 = @ 1 OR @ 4 OR @ 7
@ 9 = @ 0 AND @ 8

对于一个完整的解决方案,您可以使用 Java Scripting API ,并借用JavaScript引擎或使用自己的小脚本语言


I have a complex string coming from the UI like:

(region = "asia") AND ((status = null) OR ((inactive = "true") AND (department = "aaaa")) OR ((costcenter = "ggg") OR (location = "india")))

I need to split it and use it in my code, but I have to take into consideration the braces so that grouping occurs exactly as shown. After split, I have to get something like the following in each iteration and break it down

First time:

(region = "asia") AND 

((status = null) OR ((inactive = "true") AND (department = "aaaa")) OR ((costcenter = "ggg") OR (location = "india")))

Second time:

(region = "asia") AND 

(

(status = null) OR 

((inactive = "true") AND (department = "aaaa")) OR 
((costcenter = "ggg") OR (location = "india"))

)

and so on...

Any pointers on how to achieve this?

解决方案

As it seems you are not willing to go in full-fledged parsing, and regex cannot tackle this kind of problem, maybe a step-wise solution.

Here a list of variables is construed, where the ith entry has the inner text value of (...) with variables of the form @123 where 123 is the i.

static String parse(String exp, List<String> vars) {
    final Pattern BRACED_REDEX = Pattern.compile("\\(([^()]*)\\)");
    for (;;) {
        Matcher m = BRACED_REDEX.matcher(exp);
        if (!m.find()) {
            break;
        }
        String value = m.group(1);
        String var = "@" + vars.size();
        vars.add(value);
        StringBuffer sb = new StringBuffer();
        m.appendReplacement(sb, var);
        m.appendTail(sb);
        exp = sb.toString();
    }
    vars.add(exp); // Add last unreduced expr too.
    return exp;
}

public static void main(String[] args) {
    String exp = "(region = \"asia\") AND ((status = null) OR ((inactive = \"true\") "
        + "AND (department = \"aaaa\")) OR ((costcenter = \"ggg\") OR "
        + "(location = \"india\")))";
    List<String> vars = new ArrayList<>();
    exp = parse(exp, vars);
    System.out.println("Root expression: " + exp);
    for (int i = 0; i < vars.size(); ++i) {
        System.out.printf("@%d = %s%n", i, vars.get(i));
    }
}

This will give

Root expression: @0 AND @8
@0 = region = "asia"
@1 = status = null
@2 = inactive = "true"
@3 = department = "aaaa"
@4 = @2 AND @3
@5 = costcenter = "ggg"
@6 = location = "india"
@7 = @5 OR @6
@8 = @1 OR @4 OR @7
@9 = @0 AND @8

For a full fledged solution you could use the Java Scripting API and either borrow the JavaScript engine or make your own small Scripting language,

这篇关于基于java中的小括号的分组拆分一个复杂的String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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