我需要RegEx的帮助。
有效值:
-1.00 D
-2,000.00 D
-100,000.56 D
-100,000.00 D
-1,123,456.43 D
1.00 C
2,000.00 C
100,000.56 C
100,000.00 C
1,123,456.43 C
十进制(整数)之前最多可以为13位数字。十进制应始终为2位数。
-ve值将有SPACE,然后是D
和
+ ve值将具有SPACE,然后是C
请帮助。
^(?=-.*D$|\d.*C$)(?=[^\d\r\n]*(?:\d[^\d\r\n]*){1,13}$)-?[1-9]\d{0,2}(?:,\d{3})*\.\d{2} [DC] *$
Demo我使用PCRE regex引擎对此进行了测试,但它可以与支持超前的任何引擎一起使用。
正则表达式以两个积极的前瞻开头。第一个断言是,如果字符串以连字符或数字开头,则如果连字符,则最后一个字符必须为
D
;如果为数字,则最后一个字符必须为C
。第二个超前断言该字符串包含1-13位数字。正则表达式引擎执行以下操作。
^ match beginning of line (?= begin positive lookahead -.*D$ match '-', 0+ chars, 'D' at end of line | or \d.*C$ match 1 digit, 0+ chars, 'C' at end of line ) end positive lookahead (?= begin positive lookahead [^\d\r\n]* match 0+ characters other than a digit, '\r' or '\n' (?:\d[^\d\r\n]*) match 1 digit followed by 0+ chars other than a digit, '\r', or '\n' {1,13}$ execute preceding non-capture group 1-13 times ) end positive lookahead -?[1-9]\d{0,2} optionally match '?', one digit other than '0', 0-2 digits (?:,\d{3}) match ',' then 3 digits in non-capture group * execute preceding non-capture group 0+ times \.\d{2} [DC] * match '.', 2 digits, 1 space 'D' or 'C', 0+ spaces $ match end of string.
^(?=-.{,21}D|\d.{,20}C)-?\d{1,3}(,\d{3})*\.\d\d [CD]$
请参见live demo。