这里是正则表达式语法的新手。尝试编写正则表达式以提供一些输入验证。
我需要的是一个匹配整数的正则表达式,或者十进制数小于一位的十进制数。
Good Match
1
12
100
1.1
100.1
1.0
No Match
1.22
1.
0
012
这是我想出来的但它不起作用:
Regex.IsMatch(paidHours, "\\d+[.]?[0-9]?")
答案 0 :(得分:3)
您可以尝试:
Regex.IsMatch(paidHours, "^\\d+(\\.\\d)?$")
答案 1 :(得分:3)
Regex.IsMatch(paidHours, @"^\d+(\.\d)?$")
答案 2 :(得分:2)
OP问题编辑后编辑的答案。
Regex.IsMatch(paidHours, @"^[1-9][0-9]*(\.[0-9])?$");
说明:
^ : Start of the String
[1-9] : A single number between 1 and 9
[0-9]* : Zero or more number(s) between 0 and 9
([0-9]? would match zero or one number and the String "100" would not match the regex)
( : Start of a group
\. : A point
[0-9] : A single number between 0 and 9
)? : End of the group. The group must be repeated zero or one time
$ : End of the String
请注意,\d
<{1}}与[0-9]
完全匹配<{1}}:\d
与任何unicode digit相匹配。例如,如果您使用௮
,则会匹配此字符\d
,但如果您使用[0-9]
则不会匹配。
答案 3 :(得分:0)
尝试指定行的开始/结束:
@"^\d+[.]?[0-9]?$"
你的正则表达式不起作用,因为1.234是匹配机智1.2,如果你没有指定你希望字符串以'$'
符号结束。