^-?[0-9]\d*(\.\d+)?$
但需要它,只允許最多3位小數。所以允許值包括:
+10.123
-10.123
10.123
10
+10
-10
10.1
10.12
不允許:
10.1234
10.123%
通知/提示理解表達MODS的。
在此先感謝。
^-?[0-9]\d*(\.\d+)?$
但需要它,只允許最多3位小數。所以允許值包括:
+10.123
-10.123
10.123
10
+10
-10
10.1
10.12
不允許:
10.1234
10.123%
通知/提示理解表達MODS的。
在此先感謝。
^[+-]?\d+(\.\d{1,3})?$
見這裏:https://www.debuggex.com/r/BbCBL5pQWLxsD4a6
^ asserts position at start of a line
Match a single character present in the list below [+-]?
? Quantifier — Matches between zero and one times, as many times as possible,
giving back as needed (greedy)
+- matches a single character in the list +- (case sensitive)
\d+ matches a digit (equal to [0-9])
+ Quantifier — Matches between one and unlimited times, as many times as possible,
giving back as needed (greedy)
1st Capturing Group (\.\d{1,3})?
? Quantifier — Matches between zero and one times, as many times as possible,
giving back as needed (greedy)
\. matches the character . literally (case sensitive)
\d{1,3} matches a digit (equal to [0-9])
{1,3} Quantifier — Matches between 1 and 3 times, as many times as possible,
giving back as needed (greedy)
$ asserts position at the end of a line
@WiktorStribiżew在您評論之前,我正在創建樣本。只有我在原來的錯誤中改變的是{0,3}到{1,3}。 –
@WiktorStribiżew現在有。 –
@dasblinkenlight在線網站爲兩者生成相同的圖片。 –
^[+-]{0,1}\d*?(\.{0,1}\d{0,3})?$
應該工作
看到https://regex101.com/r/P6DBrW/1/的正則表達式
'{0,3}'會讓您匹配具有零十進制數字的'10.'。 OP的正則表達式不允許這樣做。 – dasblinkenlight
非常感謝。 – SamJolly
除了*
和+
元字符,它指定無限重複的說明,正則表達式允許您將在比賽用一些具體的限制{a,b}
構造。在此,a
是所需的最小匹配數,而b
是最大值。 a
和b
均爲,包括。
既然你需要搭配最多三個數字至少一個,你需要\d{1,3}
更換\d+
:
^[+-]?[0-9]\d*(\.\d{1,3})?$
優化:在手工作正則表達式,您可以通過更換[0-9]
優化與另一\d
,和 「摺疊」 成\d*
通過使用\d+
:
^[+-]?\d+(\.\d{1,3})?$
真的很感謝你的答案,只是可以打勾2個答案!謝謝。 – SamJolly
這裏:'^ [ - +] \ d +(\ \ d {1,3})?$'' –