正则表达式验数字和小数
时间: 2023-11-27 13:48:20 浏览: 61
JS中验证整数和小数的正则表达式
以下是两个验证数字和小数的正则表达式:
1. 验证数字:^[0-9]*$
```python
import re
pattern = "^[0-9]*$"
result = re.match(pattern, "123456") # 匹配成功
result = re.match(pattern, "12a3456") # 匹配失败
```
2. 验证有两位小数的正实数:^[0-9]+(\.[0-9]{2})$
```python
import re
pattern = "^[0-9]+(\.[0-9]{2})$"
result = re.match(pattern, "123.45") # 匹配成功
result = re.match(pattern, "123.456") # 匹配失败
```
阅读全文