我正在做一个正则表达式任务,需要创建一个正则表达式来识别文本文件>表
所有具有第一列以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);
?>
正则表达式结果
答案 0 :(得分:5)
不需要preg_match_all,preg_match隐含g
标志,如果要对它们进行求和,则必须捕获行末的值。
使用:/VI-.+?\((?:PP|DB)\).+?\$(\d+(?:\.\d\d)?)/
说明:
/ # regex delimiter
VI- # literally VI-
.+? # 1 or more any character but newline, not greedy
\( # opening parenthesis
(?: # non capture group
PP|DB # PP or DB
) # end group
\) # closing parenthesis
.+? # 1 or more any character but newline, not greedy
\$ # $ sign
( # start group 1
\d+ # 1 or more digits
(?: # non capture group
\.\d\d # a dot and 2 digits
)? # end group, optional
) # end group 1
/ # regex delimiter
代码:
$file = fopen("file.txt", "r") or die("Cannot open file!\n");
$regex = '/VI-.+?\((?:PP|DB)\).+?\$(\d+(?:\.\d\d)?)/';
$total = 0;
while ($line = fgets($file, 1024)) {
if (preg_match($regex, $line, $matches)) {
$total += $matches[1];
}
}
fclose($file);
echo $total,"\n";
输出(对于给定的示例):
20.25