2016-08-24 175 views
1

我有如下的字符串:提取長字符串的一部分。

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> 

注意:&可能有單次或多次出現。

我嘗試過模式匹配。

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

它正在整條線。我如何使它更精確。

+0

喜歡的東西[**'([\ W] +。):\ d + \:\ d + =([^ ,] +)'**](https://regex101.com/r/lD2iO9/1)? – Jan

+1

^測試很快並沒有給我任何結果...沒有太多時間來找到可以工作的正則表達式,但想想正則表達式測試網站來建立你的,OP(https://regex101.com /例如) –

+1

類似[this](https://regex101.com/r/oJ6jZ9/1)? –

回答

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, 
+0

謝謝,它對我的​​預期效果。 :) – User27854