提取长字符串的一部分。

时间:2016-08-24 13:12:39

标签: java regex pattern-matching

我的字符串如下:

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

它在一条线上(我已经做了上述安排以便于理解)。我只需要提取这些

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

即它具有流动的格式。

<fields>:<SomeNumber>:<SomeNumber>=<Id>}<Message><&amp>;{<values>,<values>

注意:&amp;可以有一次或多次出现。

我尝试过模式匹配。

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 &amp;{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");
}

它正在走整条路线。如何使其更精确。

1 个答案:

答案 0 :(得分:2)

试试此代码

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  &amp;{at Station Bangalore city,Accomodation:1:1=Hotel Booking }Hotel Booking is not &amp;{done,Travel:1:1=Cab}Destination  &amp;{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"); 
}

它产生以下输出

Food:1:1=Veg North }North Indian Food  &amp;{at Station Bangalore city,
Accomodation:1:1=Hotel Booking }Hotel Booking is not &amp;{done,
Travel:1:1=Cab}Destination  &amp;{Hotel  Capitol,