gpt4 book ai didi

java - 将表达式 String 拆分为 ArrayList

转载 作者:行者123 更新时间:2023-11-29 08:36:26 25 4
gpt4 key购买 nike

我有一个表达式字符串为

"((firstname = john AND Lastname = Eleven) OR (salary = 15000 AND location = Mexico OR (firstname = mathew AND lastname = Thirteen)))";

是否有任何内置函数我可以将它们拆分为 List<String>具有以下格式:

list[0] = "(";
list[1] = "(";
list[2] = "firstname";
list[3] = "=";
list[4] = "john"
list[5] = "AND";
list[6] = "Lastname"
list[7] = "=";
list[8] = "Eleven";
list[9] = ")"
list[10] = "OR"
list[11] = "(";
list[12] = "salary";
list[13] = "15000"
list[14] = "AND"
etc...

最佳答案

您可以将所有 () 替换为 space $1 space 然后用空格拆分:

String spl[] = 
str.replaceAll("([\\(\\)=])", " $1 ").replaceAll(" +", " ").trim().split("\\s");

输出

(, (, firstname, =, john, AND, Lastname, =, Eleven, ), OR, (, salary, =, 15000, AND, 
location, =, Mexico, OR, (, firstname, =, mathew, AND, lastname, =, Thirteen, ), ), )

关于java - 将表达式 String 拆分为 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43802262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com