Я работаю над задачей регулярного выражения, где мне нужно создать регулярное выражение для идентификации в текстовом файле> таблица
Все строки, имеющие первый столбец, начинающийся с VI-
и заканчивающийся (PP)
или (DB)
, извлекают и складывают абсолютные значения для последнего столбца (т. Е. 0,73 + 0,11…).
Затем выведите итоговое значение.
Вот прикрепленный текстовый файл.
FEES Amount charged to authorize, process and settle card transactions, along with transaction-based and/or fixed amounts charged for specific card processingservices.
MC-WORLDCARD RESTAURANT Interchange charges -$13.85
MC-CORP T & E I(US) BUS Interchange charges -$0.85
MC-CORP T & E I(US) CORP Interchange charges -$3.18
MC-WORLD ELITE RESTAURANT Interchange charges -$22.02
MC-HIGH VAL RESTAURANT Interchange charges -$2.16
MC-DOMESTIC MERIT III Interchange charges -$3.74
MC-RESTAURANT (DB) Interchange charges -$2.22
MC-DOMESTIC MERIT III (DB) Interchange charges -$2.03
MASTERCARD SALES DISCOUNT .006 DISC RATE TIMES $2743.61 Service charges -$16.46
MC LICENSE VOLUME FEE .000061 DISC RATE TIMES $3143.14 Service charges -$0.19
MASTERCARD DEBIT SALES DISC .006 DISC RATE TIMES $399.53 Service charges -$2.40
MASTERCARD AUTH FEE 96 TRANSACTIONS AT .05 Fees -$4.80
MC NETWORK ACCESS AUTH FEE 96 TRANSACTIONS AT .0195 Fees -$1.87
VISA
VI-US REGULATED COMM (DB) Interchange charges -$0.51
VI-CPS SMALL TICKET (PP) Interchange charges -$0.11
VISA ASSESSMENT FEE CR .0014 TIMES $6964.33 Interchange charges -$9.75
VISA ASSESSMENT FEE DB .0013 TIMES $2168.68 Interchange charges -$2.82
VI-CPS/RESTAURANT (DB) Interchange charges -$7.77
VI-CORPORATE TRAVEL SVC Interchange charges -$2.73
VI-CPS/RESTAURANT CREDIT Interchange charges -$7.23
VI-PURCHASING TRAVEL SVC Interchange charges -$1.23
VI-ELECTRONIC (US ACQ) Interchange charges -$0.46
VI-INTER PREM LAC ISS US ACQ Interchange charges -$3.13
VI-SIGNATURE PREFERRED CRP ELC Interchange charges -$60.70
VI-SIGNATURE CARD ELECTRONIC Interchange charges -$52.58
VI-BUSINESS CARD TR2 ELEC T&E Interchange charges -$9.21
VI-BUSINESS CARD TR4 ELEC Interchange charges -$8.97
VI-BUSINESS CARD CP (DB) Interchange charges -$0.54
VI-CPS/RESTAURANT (PP) Interchange charges -$0.73
VI-CPS/SMALL TICKET Interchange charges -$3.62
VI-BUSINESS CARD TR1 ELEC T&E Interchange charges -$6.32
VI-BUSINESS CARD TR3 ELEC T&E Interchange charges -$7.46
VI-CPS SMALL TICKET (DB) Interchange charges -$1.12
VI-US REGULATED (DB) Interchange charges -$7.89
VI-CPS/REWARDS 2 Interchange charges -$16.87
VI-US HNW CONSUMER ELECT Interchange charges -$0.81
VI-US CPS/SMALL TCKT REG (DB) Interchange charges -$1.58
VISA DEBIT SALES DISCOUNT .006 DISC RATE TIMES $2168.68 Service charges -$13.01
VISA SALES DISCOUNT .006 DISC RATE TIMES $6964.33 Service charges -$41.79
VISA AUTH FEE 280 TRANSACTIONS AT .05 Fees -$14.00
ACQUIRER PROCESSOR FEE DB/PP 65 TRANSACTIONS AT .0155 Fees -$1.01
ACQUIRER PROCESSOR FEE CREDIT 212 TRANSACTIONS AT .0195 Fees -$4.13
DISCOVER
DSCVR PSL REST PR Interchange charges -$4.01
DSCVR PSL REST PP Interchange charges -$0.86
DISCOVER ASSESSMENT FEE .0013 TIMES $960.98 Interchange charges -$1.25
DSCVR COMML ELECT OTHER Interchange charges -$4.06
DSCVR PSL EXP SVC PR Interchange charges -$0.62
DSCVR PSL EXP SVC RW Interchange charges -$1.62
DSCVR PSL REST RW Interchange charges -$10.91
DISCOVER SALES DISCOUNT .006 DISC RATE TIMES $960.98 Service charges -$5.77
DISCOVER DATA USAGE FEE 35 TRANSACTIONS AT .0195 Service charges -$0.68
DISCOVER AUTH FEE 35 TRANSACTIONS AT .05 Fees -$1.75
NETWORK AUTHORIZATION FEE 35 TRANSACTIONS AT .0025 Fees -$0.09
AMERICAN EXPRESS
AMEX AUTH FEE 17 TRANSACTIONS AT .05 Fees -$0.85
Вот код php.
<?php
$file = fopen("sampledata.txt", "r") or die("Cannot open file!\n");
$regex = "/VI-\w.+?(\(PP\)|\(DB\))+/g"; // regex, but it selected the individual row > field. see the sreenshot.
$total = 0;
while ($line = fgets($file, 1024)) {
preg_match_all($regex, $line, $matches, PREG_OFFSET_CAPTURE);
if (count($matches) > 0) {
// sum the matching value.
} else {
echo "No match: ";
}
}
fclose($file);
print_r($total);
?>
Результат регулярного выражения