Reputation: 884
I am having a string as below:
FX1511237205/162370000000933.00/1/Train,Train.Time:1:1=MysoreExp,Station:1:1=Cantonment,
Carts.AC:1:1=05,Currency.Transaction:1:1=INR,Station.Destination:1:1=Bangalore City,
Total.Fare:1:1=35.00,Date.Booked:1:1=20150422,Date.Travel:1:1=20160517,Seat.Remaining:1:1=4,
Food:1:1=Veg North }North Indian Food &{at Station Bangalore city,
Accomodation:1:1=Hotel Booking }Hotel Booking is not &{done,
Travel:1:1=Cab}Destination &{Hotel Capitol,No.Passenger:1:1=5,Booking:1:1=Success
Its in a single line (I have made the above arrangement for easy understanding). I need to extract only these
Food:1:1=Veg North }North Indian Food &{at Station Bangalore city,
Accomodation:1:1=Hotel Booking }Hotel Booking is not & &{done{ for anydate,
Travel:1:1=Cab}Destination &{Hotel Capitol
That is it has the flowing format.
<fields>:<SomeNumber>:<SomeNumber>=<Id>}<Message><&>;{<values>,<values>
Note: & can have single or multiple occurrences.
I have tried with pattern matching.
String line="FX1511237205/IFTEST162370000000933.00/1/FOREX,DEAL.TYPE:1:1=SP,COUNTERPARTY:1:1=100471,DEALER.DESK:1:1=00,CURRENCY.MARKET:1:1=1,CURRENCY.BOUGHT:1:1=USD,AMOUNT.BOUGHT:1:1=10.00,VALUE.DATE.BUY:1:1=20150422,CURRENCY.SOLD:1:1=GBP,AMOUNT.SOLD:1:1=5.41,VALUE.DATE.SELL:1:1=20150422,SPOT.RATE:1:1=1.85,LIMIT.REFERENCE.NO:1:1=1010.01,POSITION.TYPE.BUY:1:1=TR,POSITION.TYPE.SELL:1:1=TR,DEAL.DATE:1:1=20150422,SPOT.DATE:1:1=20150424,BASE.CCY:1:1=GBP,SPOT.LCY.AMOUNT:1:1=10.00,OUR.ACCOUNT.PAY:1:1=23701,OUR.ACCOUNT.REC:1:1=23752,DEL.DATE.BUY:1:1=20150422,DEL.AMOUNT.BUY:1:1=10.00,DEL.DATE.SELL:1:1=20150422,DEL.AMOUNT.SELL:1:1=5.41,CPARTY.CORR.NO:1:1=120048,PAY.ACC.POSTED:1:1=00:15:33 24 AUG 2016,REC.ACC.POSTED:1:1=00:15:33 24 AUG 2016,BUY.LCY.EQUIV:1:1=-10.00,SEL.LCY.EQUIV:1:1=10.00,SWIFT.COMMON.REF:1:1=BOFA330185DEMOPX,CATEGORY.CODE:1:1=20010,FX.GROUP.COND.ID:1:1=999,ACCOUNT.OFFICER:1:1=5,FED.FUNDS:1:1=C,SEND.CONFIRMATION:1:1=NORMAL,SEND.PAYMENT:1:1=NORMAL,SEND.ADVICE:1:1=NORMAL,TRANSACTION.TYPE:1:1=SP,NETTING.STATUS:1:1=N,AMORTISE.POSITION:1:1=NO,SOD.MAT:1:1=NO,CLS.DEAL:1:1=NO,PRE.UTI.ID.1:1:1=VAL,PRE.UTI.ID.2:1:1=FX-SPOT.RATE.EXCEEDS.TOLERANCE}SPOT RATE EXCEEDS TOLERANCE BY &{25.54%,EXEC.TIME.STAMP:1:1=INAU,CP.TRADE.PURPOSE:1:1=1,TRADE.REPOSITORY:1:1=52379_INPUTTER__OFS_IFPA,UNIQUE.PROD.ID:1:1=1608240015,RESERVED9:1:1=GB0010001,RESERVED8:1:1=1";
String startpattern="";
String pattern="(.*)(=)(.*)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
if (m.find( )) {
System.out.println("Found value: " + m.group(0) );
} else {
System.out.println("NO MATCH");
}
Its is taking the entire line. How do I make it more precise.
Upvotes: 1
Views: 61
Reputation: 511
Try this code
String line="FX1511237205/162370000000933.00/1/Train,Train.Time:1:1=MysoreExp,Station:1:1=Cantonment,Carts.AC:1:1=05,Currency.Transaction:1:1=INR,Station.Destination:1:1=Bangalore City,Total.Fare:1:1=35.00,Date.Booked:1:1=20150422,Date.Travel:1:1=20160517,Seat.Remaining:1:1=4,Food:1:1=Veg North }North Indian Food &{at Station Bangalore city,Accomodation:1:1=Hotel Booking }Hotel Booking is not &{done,Travel:1:1=Cab}Destination &{Hotel Capitol,No.Passenger:1:1=5,Booking:1:1=Success";
String pattern="(\\w+:\\d+:\\d+\\=[\\w|\\s]+\\}[\\w|\\s|\\&|\\;]+\\{[\\w|\\s]+\\,)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
if (m.find()) {
System.out.println(m.group());
while (m.find( )) {
System.out.println(m.group());
}
}
else {
System.out.println("NO MATCH");
}
It produces the following output
Food:1:1=Veg North }North Indian Food &{at Station Bangalore city,
Accomodation:1:1=Hotel Booking }Hotel Booking is not &{done,
Travel:1:1=Cab}Destination &{Hotel Capitol,
Upvotes: 2