判断如果一个字符串是数字,-,换行,python
时间: 2023-12-14 22:05:37 浏览: 28
我可以为您提供一个Python代码段,用于判断一个字符串是否由数字、连字符、换行符和单词"python"组成:
```python
def is_valid_string(s):
valid_chars = set('0123456789-\npython')
return set(s) <= valid_chars
```
您可以将要检查的字符串作为参数传递给 is_valid_string 函数,此函数将返回 True 如果字符串仅由数字、连字符、换行符和单词 "python" 组成,否则将返回 False。
相关问题
令字符串 s='马丽于2022-4-29 10:10刊发了一首诗《Listen to me》,诗词的大意是:\n Mama you taught me to do the right things,\n So now you have to let your baby fly.\n You've given me everything that I will need,\n To make it through this crazy thing called life,\n Thank you Mum!' ,通过编写函数实现下述各题功能。 1 应用正则表达式判断该字符串是否包含0~9的数字和大小写英文字母。 2 根据正则表达式使用中文逗号、中文句号、英文逗号、英文点号、英文感叹号、英文换行符号将该字符串分割成多个子串。 3 应用正则表达式提取该字符串中的日期时间字符串。 4 应用正则表达式提取该字符串中的诗词名称。 5 应用正则表达式提取该字符串中的姓名。
1. 使用re模块判断字符串是否包含0~9的数字和大小写英文字母:
```python
import re
s = '马丽于2022-4-29 10:10刊发了一首诗《Listen to me》,诗词的大意是:\n Mama you taught me to do the right things,\n So now you have to let your baby fly.\n You\'ve given me everything that I will need,\n To make it through this crazy thing called life,\n Thank you Mum!'
pattern = re.compile('[0-9a-zA-Z]+')
result = pattern.findall(s)
if result:
print('字符串中包含数字和英文字母')
else:
print('字符串中不包含数字和英文字母')
```
2. 使用正则表达式将该字符串分割成多个子串:
```python
import re
s = '马丽于2022-4-29 10:10刊发了一首诗《Listen to me》,诗词的大意是:\n Mama you taught me to do the right things,\n So now you have to let your baby fly.\n You\'ve given me everything that I will need,\n To make it through this crazy thing called life,\n Thank you Mum!'
pattern = re.compile('[,,.。!!\n]')
result = pattern.split(s)
print(result)
```
3. 使用正则表达式提取该字符串中的日期时间字符串:
```python
import re
s = '马丽于2022-4-29 10:10刊发了一首诗《Listen to me》,诗词的大意是:\n Mama you taught me to do the right things,\n So now you have to let your baby fly.\n You\'ve given me everything that I will need,\n To make it through this crazy thing called life,\n Thank you Mum!'
pattern = re.compile('\d{4}-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}')
result = pattern.findall(s)
print(result)
```
4. 使用正则表达式提取该字符串中的诗词名称:
```python
import re
s = '马丽于2022-4-29 10:10刊发了一首诗《Listen to me》,诗词的大意是:\n Mama you taught me to do the right things,\n So now you have to let your baby fly.\n You\'ve given me everything that I will need,\n To make it through this crazy thing called life,\n Thank you Mum!'
pattern = re.compile('《(.+?)》')
result = pattern.findall(s)
print(result)
```
5. 使用正则表达式提取该字符串中的姓名:
```python
import re
s = '马丽于2022-4-29 10:10刊发了一首诗《Listen to me》,诗词的大意是:\n Mama you taught me to do the right things,\n So now you have to let your baby fly.\n You\'ve given me everything that I will need,\n To make it through this crazy thing called life,\n Thank you Mum!'
pattern = re.compile('[\u4e00-\u9fa5]{2,3}')
result = pattern.findall(s)
print(result)
```
使用Python,编程以下问题:令字符串 s='马丽于2022-4-29 10:10刊发了一首诗《Listen to me》,诗词的大意是:\n Mama you taught me to do the right things,\n So now you have to let your baby fly.\n You've given me everything that I will need,\n To make it through this crazy thing called life,\n Thank you Mum!' ,通过编写函数实现下述各题功能。 1 应用正则表达式判断该字符串是否包含0~9的数字和大小写英文字母。 2 根据正则表达式使用中文逗号、中文句号、英文逗号、英文点号、英文感叹号、英文换行符号将该字符串分割成多个子串。 3 应用正则表达式提取该字符串中的日期时间字符串。 4 应用正则表达式提取该字符串中的诗词名称。 5 应用正则表达式提取该字符串中的姓名。
1.
import re
def has_alpha_digit(s):
return bool(re.search('[A-Za-z0-9]', s))
s='马丽于2022-4-29 10:10刊发了一首诗《Listen to me》,诗词的大意是:\n Mama you taught me to do the right things,\n So now you have to let your baby fly.\n You\'ve given me everything that I will need,\n To make it through this crazy thing called life,\n Thank you Mum!'
print(has_alpha_digit(s))
2.
import re
def split_strings(s):
return re.split('[,。,.;!\n]+', s)
s='马丽于2022-4-29 10:10刊发了一首诗《Listen to me》,诗词的大意是:\n Mama you taught me to do the right things,\n So now you have to let your baby fly.\n You\'ve given me everything that I will need,\n To make it through this crazy thing called life,\n Thank you Mum!'
print(split_strings(s))
3.
import re
def extract_datetime(s):
return re.findall('\d{4}-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}', s)
s='马丽于2022-4-29 10:10刊发了一首诗《Listen to me》,诗词的大意是:\n Mama you taught me to do the right things,\n So now you have to let your baby fly.\n You\'ve given me everything that I will need,\n To make it through this crazy thing called life,\n Thank you Mum!'
print(extract_datetime(s))
4.
import re
def extract_poem_name(s):
return re.findall('《([^《》]+)》', s)
s='马丽于2022-4-29 10:10刊发了一首诗《Listen to me》,诗词的大意是:\n Mama you taught me to do the right things,\n So now you have to let your baby fly.\n You\'ve given me everything that I will need,\n To make it through this crazy thing called life,\n Thank you Mum!'
print(extract_poem_name(s))
5.
import re
def extract_name(s):
return re.findall('([^\n]+)你', s)[0]
s='马丽于2022-4-29 10:10刊发了一首诗《Listen to me》,诗词的大意是:\n Mama you taught me to do the right things,\n So now you have to let your baby fly.\n You\'ve given me everything that I will need,\n To make it through this crazy thing called life,\n Thank you Mum!'
print(extract_name(s))