簡體   English   中英

Java使用Regex表達式分割字符串

[英]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 "分割字符串" and "因為我們在字符串中有兩個(一個是必需的,另一個不是必需的)

我想把它分成這樣

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

但這是行不通的,因為它將從每個分割中刪除端括號]

根據以下正則表達式拆分輸入。

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

這會根據and分割輸入,而輸入正好在正斜杠之前退出。

您必須在此處使用后向功能:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM