一、使用input函数从控制台上接收一个不包含汉字的任意字符串,使用正则表 达式进行如下验证: 1.验证该字符串是否为手机号。(手机号要求:1开头,第二个数字为3、4、 5、7、8、9中的任意一个,第三到十一位为任意数字)。
时间: 2024-09-26 21:07:15 浏览: 46
在Python中,你可以使用`re`模块来进行正则表达式的匹配。首先,你需要导入`re`模块,然后编写一个正则模式来匹配手机号的要求。下面是一个例子:
```python
import re
def is_valid_phone_number(input_string):
# 正则表达式模式,匹配以1开头,第二位是3、4、5、7、8、9之一,后面跟着0-9共10位数字的格式
pattern = r'^1(3|4|5|7|8|9)\d{8}$'
if re.match(pattern, input_string):
return True
else:
return False
# 获取用户输入
phone_input = input("请输入手机号码:")
# 验证手机号码
if is_valid_phone_number(phone_input):
print("手机号码有效")
else:
print("手机号码无效")
```
在这个代码中,`^` 表示字符串开始,`\d` 匹配任意数字,`{n}` 表示前面的字符重复n次。`$` 表示字符串结束,保证了整个输入都是10位数字。
相关问题
编写一个Java程序,从控制台接收一个字符串,验证该字符串是否为有效的手机号码。假设有效的手机号码格式为以1开头,后面跟着10位数字。使用正则表达式 样例: 输入:1234567890 输出:No
在Java中,你可以使用`java.util.regex`包下的正则表达式来验证一个字符串是否符合特定的手机号格式。以下是一个简单的Java程序示例:
```java
import java.util.Scanner;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个手机号码(以1开头,11位数字): ");
String input = scanner.nextLine();
// 正则表达式模式匹配手机号码
Pattern pattern = Pattern.compile("\\d{11}");
boolean isMatch = pattern.matcher(input).matches();
if (isMatch) {
System.out.println("Yes, it's a valid phone number.");
} else {
System.out.println("No, it's not a valid phone number.");
}
}
}
```
在这个程序中,我们首先创建了一个`Scanner`对象用于从控制台读取用户输入。然后,使用正则表达式`\d{11}`,它代表连续的11个数字。`pattern.compile()`用于编译这个模式,`matcher(input)`创建了一个匹配器,最后`matches()`函数检查输入的字符串是否完全匹配此模式。
用python写一段程序,要求如下:(1)请一段英文报道从键盘输入,并保存到c:\myFile.txt (2) 打开该文件,统计单词出现的次数 提示:选择合适的正则表达式,分割字符串: re.split('\.| |,|\n',a)) 对于数字可选择函数判断是不是数字,或者采用if语句判断并过滤。对于openKylin 1.0也可采用类似的方法处理。 如果碰到单词的变化形式,则按照原型进行统计。可以建立如下形式的字典: {led:lead, leading:lead, …} 或者 {(led, leading): lead,…}或者 {lead:[led, leading],…}或者 {(lead, leading, led):值,...} (3) 建立4级单词集合,统计4级单词出现的次数 假设4级单词如下: roll indicate develop operate
下面是根据您的要求编写的Python程序:
```python
import re
# (1) 从键盘输入英文报道,并保存到文件
def save_report():
report = input("请输入英文报道:")
file_path = 'c:\\myFile.txt'
with open(file_path, 'w') as file:
file.write(report)
print("报道已保存至", file_path)
# (2) 统计单词出现的次数
def count_word_frequency():
word_count = {}
file_path = 'c:\\myFile.txt'
with open(file_path, 'r') as file:
content = file.read()
# 使用正则表达式分割字符串并过滤数字和标点符号
words = re.split('\.| |,|\n', content)
for word in words:
# 过滤数字
if word.isdigit():
continue
# 过滤标点符号和空字符串
if not word.isalpha():
continue
# 统计单词出现的次数,使用原型作为键
word = word.lower()
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
print("单词出现的次数统计:")
for word, count in word_count.items():
print(word, ":", count)
# (3) 统计4级单词出现的次数
def count_level_4_words():
level_4_words = ['roll', 'indicate', 'develop', 'operate']
level_4_count = {}
file_path = 'c:\\myFile.txt'
with open(file_path, 'r') as file:
content = file.read()
# 使用正则表达式分割字符串并过滤数字和标点符号
words = re.split('\.| |,|\n', content)
for word in words:
# 过滤数字
if word.isdigit():
continue
# 过滤标点符号和空字符串
if not word.isalpha():
continue
# 统计4级单词出现的次数
word = word.lower()
if word in level_4_words:
if word in level_4_count:
level_4_count[word] += 1
else:
level_4_count[word] = 1
print("4级单词出现的次数统计:")
for word, count in level_4_count.items():
print(word, ":", count)
# 调用函数进行测试
save_report()
count_word_frequency()
count_level_4_words()
```
这个程序首先会从键盘输入英文报道,并将其保存到文件 `c:\myFile.txt` 中。然后,它会打开该文件,使用正则表达式分割字符串并过滤数字和标点符号,统计单词的出现次数,并使用原型作为键。最后,它会统计4级单词('roll', 'indicate', 'develop', 'operate')的出现次数。
请确保运行程序前已经创建了 `c:\myFile.txt` 文件,并在运行程序后检查控制台输出以查看单词出现次数统计结果。
阅读全文
相关推荐
















