简体   繁体   English

如何通过逻辑运算符(和或)在Java中拆分字符串,但是如果它们出现在引号中则不考虑它们?

[英]How to split a string in java by logical operators (and, or) but not consider them if they appear in quotes?

I have a string that I need to split by logical operators "and", "or" (case insensitive). 我有一个字符串,需要按逻辑运算符“和”,“或”(不区分大小写)进行拆分。 However, I should not be considering these logical operator patterns if they appear in quotes, single or double. 但是,如果这些逻辑运算符模式用单引号或双引号引起来,则我不应该考虑它们。 A sample pattern: 样本模式:

contains(field1,'sample') or contains(field2,'aaandbb') AND (field3 gt 5000) contains(field1,'sample')或contains(field2,'aaandbb')AND(field3 gt 5000)

The output of the split I am trying to achieve: 我正在尝试实现的拆分输出:

contains(field1,'sample') 含有(FIELD1, '样本')

contains(field2,'aaandbb') 含有(FIELD2, 'aaandbb')

(field3 gt 5000)} (field3 gt 5000)}

Note: Please ignore the brackets. 注意:请忽略括号。

My code: 我的代码:

String soregex1="\\s+(?i)(and)|(or)\\s+";
    String[] splitStr = so1.split(soregex1);
    for(String str1:splitStr) {
        System.out.println(str1);
    }

All good except when pattern, that is, conditional operators, start appearing as values to string conditions. 除了模式(即条件运算符)开始作为字符串条件的值出现以外,其他所有方法都很好。 For example: 例如:

contains(field1,'sam or ple') or contains(field2,'aa and bb') AND (field3 gt 5000) contains(field1,'sam或ple')或contains(field2,'aa和bb')AND(field3 gt 5000)

The output for above string with my code is: 以上代码与我的代码的输出是:

contains(field1,'sam 包含(FIELD1,“山姆

ple') PLE“)

contains(field2,'aa 包含(场2,'AA

bb') BB')

(field3 gt 5000) (field3 GT 5000)

instead of 代替

contains(field1,'sam or ple') 包含(field1,'sam或ple')

contains(field2,'aa and bb') 包含(field2,'aa和bb')

(field3 gt 5000) (field3 GT 5000)

I also need to factor in escaped singleor double quotes. 我还需要考虑转义的单引号或双引号。 Appreciate any suggestions on how to avoid considering pattern matches that appear in single quotes or double quotes. 感谢有关如何避免考虑单引号或双引号中出现的模式匹配的任何建议。

have you tried this: 你尝试过这个吗?

(\\)\\s*(AND))|(\\)\\s*(OR))

demo 演示

This is a bit wild and crazy, but why not match the tokens themselves instead of splitting on the delimiter. 这有点疯狂,但是为什么不匹配标记本身而不是在定界符上进行拆分。 (I'm assuming the (...) is mandatory. Case insensitive match) (我假设(...)是必需的。不区分大小写的匹配)

\\w*\\(.*?\\)(?=\\s*(?:and|or|$))

( demo ) 演示

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

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