簡體   English   中英

自定義價格過濾器的正則表達式

[英]Regular Expression for custom price filter

我正在為特定格式的價格制定正則表達式。

有效輸入為:

  • 自然數
  • 小數(小數點后兩位),0.0 和 0.00 除外
  • 以上所有標准均不帶前導零(前 05 和 08.45 無效,但 5 和 8.45 有效)

有效測試用例示例

1
11
0.10
0.01
1.1
1.00
11.11

無效測試用例示例

0
0.
0.0
0.00
0.000
01
001
1.
1.111
1111.1111
00.1
00.11
01.11

這是我嘗試過但無法正常工作的方法

/^(((?!0)\d+)(.\d{1,2})?)|(0\.(([1-9]\d)|(0[1-9])))$/gm

請參閱演示 #1

/^[1-9]+[0-9]*(.\d{1,2})?$/gm

演示#2

貨幣正則表達式中的解決方案對我不起作用,因為它成功驗證了00.11

編輯:我想有些擺弄,這次我做對了。 只需將[1-9]更改為[0-9]即可。

^(?![0.]*$|0+[0-9])\d+(?:\.\d{1,2})?$

演示#3

感謝Wiktor Stribiżew幫助這個新手。 非常感謝先生。

利用

^(?!0+(?:\.0+)?$|0+\d)\d+(?:\.\d{1,2})?$

證明

解釋

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    0+                       '0' (1 or more times (matching the most
                             amount possible))
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (optional
                             (matching the most amount possible)):
--------------------------------------------------------------------------------
      \.                       '.'
--------------------------------------------------------------------------------
      0+                       '0' (1 or more times (matching the
                               most amount possible))
--------------------------------------------------------------------------------
    )?                       end of grouping
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    0+                       '0' (1 or more times (matching the most
                             amount possible))
--------------------------------------------------------------------------------
    \d                       digits (0-9)
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  \d+                      digits (0-9) (1 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    \d{1,2}                  digits (0-9) (between 1 and 2 times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM