Java 使用正则表达式拆分字符串

Java Split a String with Regex expression

我不太了解正则表达式。那么你能告诉我如何拆分下面的字符串以获得所需的输出吗?

String ruleString= "/Rule/Account/Attribute[N='accountCategory' and V>=1]"+
                " and /Rule/Account/Attribute[N='accountType' and V>=34]"+
                  " and /Rule/Account/Attribute[N='acctSegId' and V>=341]"+
                   " and /Rule/Account/Attribute[N='is1sa' and V>=1]"+
                    " and /Rule/Account/Attribute[N='isActivated' and V>=0]"+
                    " and /Rule/Account/Attribute[N='mogId' and V>=3]"+
                    " and /Rule/Account/Attribute[N='regulatoryId' and V>=4]"+
                    " and /Rule/Account/Attribute[N='vipCode' and V>=5]"+
                    " and /Rule/Subscriber/Attribute[N='agentId' and V='346']​";

期望的输出:

a[0] = /Rule/Account/Attribute[N='accountCategory' and V>=1]

a[1] = /Rule/Account/Attribute[N='accountType' and V>=34]
.
.
.

a[n] = /Rule/Subscriber/Attribute[N='agentId' and V='346']

我们不能简单地使用 " and " 拆分字符串,因为我们在字符串中有两个(一个是必需的,另一个不是)

我想拆分成这样

String[] splitArray= ruleString.split("] and ");

但这行不通,因为它会从每个拆分中删除结束括号 ]

根据以下正则表达式拆分您的输入。

String[] splitArray= ruleString.split("\s+and\s+(?=/)");

这根据正斜杠之前退出的 and 拆分输入。

你必须在这里使用后视:

String[] splitArray= ruleString.split("(?<=\])\s*and\s*");