简体   繁体   English

从方括号Java正则表达式中提取文本

[英]To extract text from square bracket java regex

How can I extract the texts within the CardType:[ ..... ] 如何提取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\\]");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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