python 假设电话号码得格式为:3个数字、1个短横线、4个数字、1个短横线、4个数字。如186-7123-4567。编写函数is Phone Num(s),判断字符串s是否匹配电话号码格式,若匹配返回True,否则返回False。若有符合格式的电话号码,则依次输出
时间: 2024-04-04 10:30:21 浏览: 10
可以使用正则表达式来匹配电话号码格式。下面是代码实现:
```python
import re
def isPhoneNum(s):
pattern = r'^\d{3}-\d{4}-\d{4}$'
match = re.match(pattern, s)
return match is not None
# 测试代码
test_str = '这是我的电话号码:186-7123-4567。请给我打电话。'
matches = re.findall(r'\d{3}-\d{4}-\d{4}', test_str)
for match in matches:
if isPhoneNum(match):
print(match)
```
运行结果为:
```
186-7123-4567
```
解释一下代码:
- `pattern` 是要匹配的正则表达式,其中 `\d` 表示数字字符,`^` 和 `$` 分别表示匹配字符串的开头和结尾。
- `re.match(pattern, s)` 尝试从字符串 `s` 的开头匹配正则表达式 `pattern`,如果匹配成功则返回一个 `Match` 对象,否则返回 `None`。
- `re.findall(r'\d{3}-\d{4}-\d{4}', test_str)` 在字符串 `test_str` 中查找所有符合格式的电话号码,返回一个列表。
- 遍历列表中的每个电话号码,如果符合格式则输出。
相关问题
用Python写一个程序,假设电话号码的模式为:3个数字,一个短横线,4个数字,一个短横线和四个数字,编写程序,用以检查输入的字符串是否匹配电话号码模式为若匹配返回True,否则返回False
好的,这是一个很简单的正则表达式匹配问题,可以使用Python的re模块来解决。以下是一个示例代码:
```python
import re
def is_phone_number(text):
pattern = re.compile(r'\d{3}-\d{4}-\d{4}')
match = pattern.search(text)
return match is not None
print(is_phone_number('123-4567-8901')) # True
print(is_phone_number('123-456-789')) # False
```
这个程序使用正则表达式 `\d{3}-\d{4}-\d{4}` 来匹配电话号码的模式,然后使用search函数来查找输入的字符串中是否存在这个模式。如果找到了,就返回True,否则返回False。
2)假设电话号码的模式为:3个数字、一个短横线、4个数字、一个短横线和4个 数字,如 186-7123-4567。编写 isPhoneNum()函数,用以检查输入的字符串是否匹配电 话号码模式,若匹配返回 True,否则返回 False。请将未完成的部分填入,实现其功能, 并在计算机上调试程序,以测试填入部分是否正确。 默认值为电话号码(s): 如果len(s)==13且s[3]==“-”且s[8]=“-”且 (1) li=S.拆分(“-”) s1=1n 对于李语: (2) 如果 s1.isdigit() = true: 打印(真正的) 其他: print( 假) 其他: (3)
```python
def isPhoneNum(s):
if len(s) == 13 and s[3] == "-" and s[8] == "-":
li = s.split("-")
if len(li) == 3:
s1, s2, s3 = li
if s1.isdigit() and s2.isdigit() and s3.isdigit():
return True
return False
```
解释:
1. 首先判断字符串长度是否为13,第4和第9个字符是否为短横线。
2. 如果满足条件,就将字符串按照短横线进行分割,得到一个列表li,其中li[0]表示前三个数字,li[1]表示中间四个数字,li[2]表示最后四个数字。
3. 判断列表li的长度是否为3,如果不是,说明输入的字符串不符合电话号码模式,直接返回False。
4. 如果li的长度为3,将li中的三个字符串依次赋值给变量s1、s2、s3。
5. 判断s1、s2、s3是否都为数字,如果都是,说明输入的字符串符合电话号码模式,返回True;否则返回False。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)