编写Python,分别输入三个字符串,验证其是否为八位数字,六位数字和网址,使用正则
时间: 2023-05-29 16:06:03 浏览: 68
表达式进行验证。
```python
import re
# 输入三个字符串
str1 = input("请输入一个八位数字:")
str2 = input("请输入一个六位数字:")
str3 = input("请输入一个网址:")
# 验证八位数字
pattern1 = r'^\d{8}$'
match1 = re.match(pattern1, str1)
if match1:
print(str1, "是八位数字")
else:
print(str1, "不是八位数字")
# 验证六位数字
pattern2 = r'^\d{6}$'
match2 = re.match(pattern2, str2)
if match2:
print(str2, "是六位数字")
else:
print(str2, "不是六位数字")
# 验证网址
pattern3 = r'^https?://[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?$'
match3 = re.match(pattern3, str3)
if match3:
print(str3, "是网址")
else:
print(str3, "不是网址")
```
输出示例:
```
请输入一个八位数字:12345678
请输入一个六位数字:123456
请输入一个网址:https://www.baidu.com
12345678 是八位数字
123456 是六位数字
https://www.baidu.com 是网址
```
阅读全文
相关推荐


















