[英]How to Split a String on mathematical operators as delimiters but escape operators inside quotes (in java)?
[英]How to split a string in java by logical operators (and, or) but not consider them if they appear in quotes?
我有一個字符串,需要按邏輯運算符“和”,“或”(不區分大小寫)進行拆分。 但是,如果這些邏輯運算符模式用單引號或雙引號引起來,則我不應該考慮它們。 樣本模式:
contains(field1,'sample')或contains(field2,'aaandbb')AND(field3 gt 5000)
我正在嘗試實現的拆分輸出:
含有(FIELD1, '樣本')
含有(FIELD2, 'aaandbb')
(field3 gt 5000)}
注意:請忽略括號。
我的代碼:
String soregex1="\\s+(?i)(and)|(or)\\s+";
String[] splitStr = so1.split(soregex1);
for(String str1:splitStr) {
System.out.println(str1);
}
除了模式(即條件運算符)開始作為字符串條件的值出現以外,其他所有方法都很好。 例如:
contains(field1,'sam或ple')或contains(field2,'aa和bb')AND(field3 gt 5000)
以上代碼與我的代碼的輸出是:
包含(FIELD1,“山姆
PLE“)
包含(場2,'AA
BB')
(field3 GT 5000)
代替
包含(field1,'sam或ple')
包含(field2,'aa和bb')
(field3 GT 5000)
我還需要考慮轉義的單引號或雙引號。 感謝有關如何避免考慮單引號或雙引號中出現的模式匹配的任何建議。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.