提取长字符串的一部分。
问题描述:
我有如下的字符串:提取长字符串的一部分。
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><&>;{<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 &{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");
}
它正在整条线。我如何使它更精确。
答
尝试此代码
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");
}
它产生以下输出
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,
+0
谢谢,它对我的预期效果。 :) – User27854
喜欢的东西[**'([\ W] +。):\ d + \:\ d + =([^ ,] +)'**](https://regex101.com/r/lD2iO9/1)? – Jan
^测试很快并没有给我任何结果...没有太多时间来找到可以工作的正则表达式,但想想正则表达式测试网站来建立你的,OP(https://regex101.com /例如) –
类似[this](https://regex101.com/r/oJ6jZ9/1)? –