Regex 如何使用正则表达式匹配此项?
我试图创建正则表达式来匹配所有的日期、描述和金额。我差不多有了,但是以“187927.42”结尾的那一行搞乱了,匹配的是“187927.42”,而不是“-2931.25” 我如何匹配“2015年12月22日”、“BNF:EEEEERE TECHNOLOGIES ID:1231 BNF BK:K OTAK MAHINDRA银行” 有限公司ID:INKKBK0000810付款日期:16 2105412 117.25小时POP服务/FXREF/TE-3-8-15“,“-2931.25”?以及所有其他交易行 下面是我提出的表达式Regex 如何使用正则表达式匹配此项?,regex,regex-lookarounds,Regex,Regex Lookarounds,我试图创建正则表达式来匹配所有的日期、描述和金额。我差不多有了,但是以“187927.42”结尾的那一行搞乱了,匹配的是“187927.42”,而不是“-2931.25” 我如何匹配“2015年12月22日”、“BNF:EEEEERE TECHNOLOGIES ID:1231 BNF BK:K OTAK MAHINDRA银行” 有限公司ID:INKKBK0000810付款日期:16 2105412 117.25小时POP服务/FXREF/TE-3-8-15“,“-2931.25”?以及所有其他交
/(\d{2}\/\d{2}\/\d{2})\s+(*?)\s+([0-9\,\-]+\.\d{2})*(?=\d\d\/$(?!\n-?[\d\.]+$)/mis
看看这个例子
更新:
因此,当数字大于1000时,添加一个需要数千分隔符的条件。但我觉得这不是最好的办法
(\-?(?!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+$))
假设:
- 每个记录由三部分组成:日期、描述和金额
- 日期的格式为
,后面总是跟一个空格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
)
现场演示:如果转账可以跨越多行,那么如果转账描述包含一个数字,然后是一个换行,然后是一个看起来非常像新转账的部分(日期+文本+数字),则始终可能会出现歧义。使用此格式处理帐户数据本身就很危险。您使用什么语言、工具或库来解析此文件?@melpomene PHP。我知道我可以用PHP找到这个,但我真的想避免它。
^ # 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
)