Ganeshja
Ganeshja

Reputation: 2765

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

Upvotes: 0

Views: 430

Answers (2)

Stephan
Stephan

Reputation: 8090

Try this:

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

Upvotes: 0

Jerry
Jerry

Reputation: 71538

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 ].

Upvotes: 1

Related Questions