十进制或空白的正则表达式

Phi*_*l P 5 regex

对于这个可能很愚蠢的问题,我很抱歉,但我试图将正则表达式放在一起,以便:

小数点前有1或2个数字,小数点后有0-6个数字的数字.但是,如果需要,我还需要允许该字段为空白.

有效示例

0.952321
1.20394
12.12
25
Blank
Run Code Online (Sandbox Code Playgroud)

无效的示例

123.45678
1.1234567
Run Code Online (Sandbox Code Playgroud)

请任何人可以帮忙吗?

Axe*_*ger 12

^(?:\d{1,2}(?:\.\d{0,6})?)?$
Run Code Online (Sandbox Code Playgroud)

应该做的伎俩.

\d     matches any digit
{n,m} specifies the number of occurrences
(?: ) creates an anonymous group
^     specifies the start of the string
$               the end of the string
?     means the group is optional
Run Code Online (Sandbox Code Playgroud)

  • @Phil P:如果其中一个答案解决了你的问题,那就upvote和/或接受最好的答案. (3认同)

mio*_*rel 2

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

管道之前的部分与空白匹配。之后的部分匹配一位或两位数字,可选地后跟句点和最多六位数字。因此?:,除非需要,否则我们不会使用捕获组。

希望这可以帮助!