如何使用RegEx进行匹配?

时间:2019-01-28 23:25:53

标签: regex regex-lookarounds

我正在尝试创建正则表达式以匹配所有日期,描述和金额。我几乎掌握了它,但以“ 187927.42”结尾的行被弄乱了,并且与“ 187927.42”而不是“ -2,931.25”匹配。

如何匹配“ 12/22/15”,“ BNF:eeeerere技术ID:1231231231 BNF BK:K OTAK MAHINDRA BANK LTD ID:INKKBK0000810 PMT DET:16 2105412 117.25小时POP服务/FXREF/TE-3-8-15"、"-2,931.25“?以及所有其他交易行?

这是我想出的表达式/(\d{2}\/\d{2}\/\d{2})\s+(.*?)\s+([0-9\,\-]+\.\d{2}) *(?=\d\d\/|$(?!\n-?[\d\.]+$))/mis

在此处查看此示例https://regex101.com/r/X9HeWv/1

12/17/15 Online Banking transfer to CHK 4958 Confirmation# 1231231231 -300.00
12/18/15 Online Banking transfer to CHK 4958 Confirmation# 1231231231 -200.00
12/18/15 THE HARTFORD DES:NWerfLSCIC ID:13975910 INDN:wesedrfr TECHNOLGOIES CO
ID:9942902727 CCD
-78.75
12/21/15 Online Banking transfer to CHK 4958 Confirmation# 1231231231 -50.00
12/22/15 WIRE TYPE:FX OUT DATE:151228 TIME:0944 ET TRN:2015122200194472 FX:INR 187927.42
64.1117 BNF:eeeerere TECHNOLOGIES ID:1231231231 BNF BK:K OTAK MAHINDRA BANK
LTD ID:INKKBK0000810 PMT DET:16 2105412 117.25 HOURS POP SERVICES /FXREF/TE-3-8-15
-2,931.25
12/22/15 Online Banking transfer to CHK 4958 Confirmation# 1231231231 -6,000.00
12/28/15 FORD CREDIT DES:FORDCREDIT ID:XXXXXXXXX INDN:werwe wer CO
ID:7587806091 PPD
-1,180.00
12/28/15 SC EVERCODER SOF DES:IAT PAYPAL ID:J222226DW9MWA INDN:werwer eeee CO
ID:XXXXXXXXXC IAT PMT INFO: WEB 000000000000000900
-9.00
12/29/15 Online Banking transfer to CHK 4958 Confirmation# 1231231231 -200.00

更新:

因此,通过增加一个条件,当数量大于一千时,要求使用千位分隔符。但是我觉得那不是最好的方法。

(\-?(?!0+\.00)(?=.{1,9}(\.|$))(?!0(?!\.))\d{1,3}(,\d{3})*(\.\d+)?)

(\d{2}\/\d{2}\/\d{2})\s+(.*?)\s+(\-?(?!0+\.00)(?=.{1,9}(\.|$))(?!0(?!\.))\d{1,3}(,\d{3})*(\.\d+)?) *(?=\d\d\/|$(?!\n+$))

https://regex101.com/r/T8sxkh/1

1 个答案:

答案 0 :(得分:3)

假设:

  • 每条记录均由三部分组成:日期,说明和金额。
  • 日期的格式为dd/dd/dd,并且后跟空格。
  • 金额始终为负。
  • 金额后总是行尾。
  • 金额使用,作为千位分隔符,并且始终包含.和两个小数位。
  • 说明中可以包含换行符,但绝不包含新记录的开头(即换行符后跟日期)。
  • 说明和金额之间可以有空格或换行符。

这是我想出的最好的方法:

^(\d{2}/\d{2}/\d{2}) (.*?)[ \n](-\d{1,3}(?:,\d{3})*\.\d\d)(?=\Z|\n\d{2}/\d{2}/\d{2} )

标记:msg

说明:

^  # beginning of line (with m flag)
(\d{2}/\d{2}/\d{2})  # date (which we capture)
[ ]  # a literal space
(.*?)  # description (can match across lines with s flag)
       # it is completely free-form; we match until we find something that looks like an amount
[ \n]  # separator between description and amount, space or newline
(-\d{1,3}(?:,\d{3})*\.\d\d)  # the actual amount
(?=  # followed by either:
  \Z  # end of the input (i.e. end-of-string or newline followed by end-of-string)
|    # or:
  \n \d{2}/\d{2}/\d{2}[ ]  # a newline followed by a date and a space, i.e. the start of a new record
)

实时演示:https://regex101.com/r/X9HeWv/2