简体   繁体   中英

To extract text from square bracket java regex

How can I extract the texts within the CardType:[ ..... ]

CardType:[ CashRebate=[true], Platinum=[true], CoBrandCard=[true]{CoBrandType:Holt Renfrew}, ChargeCard=[true], ConsumerCard=[true], Product Type Code:null ]

Initially i tried with following piece of code

pattern p = Pattern.compile("CardType:\\[(.*?)\\]");
Matcher m = p.matcher(value);

m getting output as

CashRebate=[true
Platinum=[true

can any one help me out please

Thanks

If you want the output to be:

CashRebate=[true], Platinum=[true], CoBrandCard=[true]{CoBrandType:Holt Renfrew}, ChargeCard=[true], ConsumerCard=[true], Product Type Code:null

Just make the regex non-lazy:

pattern p = Pattern.compile("CardType:\\[(.*)\\]");
Matcher m = p.matcher(value);

This makes the regex match the last instance of ] .

尝试这个:

pattern p = Pattern.compile("CardType:\\[\s(.*?)\s\\]");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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