令字符串 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 应用正则表达式提取该字符串中的姓名。

时间: 2023-06-01 07:04:30 浏览: 35
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) ```

相关推荐

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))
### 回答1: 可以使用以下命令构造json数据: time='2022-01-01 10:10:10.000' json_data='{"time":"'"$time"'"}' 这样就可以得到一个名为time,值为"2022-01-01 10:10:10.000"的json数据了。 ### 回答2: 在shell中构造JSON数据,可以使用echo命令和变量来实现。假设你要构造一个名为time的变量,其时间格式为2022-01-01 10:10:10.000。 首先,定义变量: time="2022-01-01 10:10:10.000" 接着,使用echo命令结合变量来构造JSON数据: echo '{"time":"'${time}'"}' 运行以上命令后,将输出以下JSON数据: {"time":"2022-01-01 10:10:10.000"} 这样你就成功在shell中构造了一个名为time,时间格式为2022-01-01 10:10:10.000的JSON数据。 ### 回答3: 在Shell中构造JSON数据并设置变量名为time,时间格式为2022-01-01 10:10:10.000,可以采取以下方法: 1. 使用变量赋值: time="2022-01-01 10:10:10.000" 2. 使用jq命令: time=$(echo '{"time": "2022-01-01 10:10:10.000"}' | jq .time) 此命令中,我们构造了一个JSON字符串 {"time": "2022-01-01 10:10:10.000"},并使用jq命令提取其中的time值,将其赋值给变量time。 3. 使用echo和sed命令: time=$(echo '{"time": "2022-01-01 10:10:10.000"}' | sed -E 's/.*"([^"]+)".*/\1/') 这个命令也是构造了一个JSON字符串 {"time": "2022-01-01 10:10:10.000"},然后使用sed命令提取其中双引号内的时间值,并赋值给变量time。 以上三种方法都可以用来构造JSON数据并将其中的时间值赋给变量time,根据具体需求选择其中一种方法即可。

最新推荐

Python字符串格式化%s%d%f详解

主要介绍了Python字符串格式化%s%d%f详解,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下

将字符串转换成gb2312或者utf-8编码的参数(js版)

直接在url中传递中文参数时,读到的中文都是乱码,那么我们应该怎么将这些参数转换呢,接下来与大家分享下将字符串转换成utf-8或者gb2312编码的参数的技巧

探讨:sql插入空,默认1900-01-01 00:00:00.000的解决方法详解

sql2005 若字段定义的类型为datetime,插入为”(空),那么会默认值为1900-01-01 00:00:00.000 解决方法查询的时候过滤下cast(nullif(”,”) as datetime) 代码如下:select cast(” as datetime) , cast(nullif(”,”)...

java基础-给出一个随机字符串,判断有多少字母?多少数字?

主要介绍了java基础-给出一个随机字符串,判断有多少字母?多少数字?文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

python简单算法04:判断一个字符串是否为回文串的排列之一

给定一个字符串,编写一个函数判定其是否为某个回文串的排列之一。 回文串是指正反两个方向都一样的单词或短语,排列是指字母重新排列,回文串不一定是字典中的单词。 例如: 输入:“tactcoa” 输出:True(排列有...

数据结构1800试题.pdf

你还在苦苦寻找数据结构的题目吗?这里刚刚上传了一份数据结构共1800道试题,轻松解决期末挂科的难题。不信?你下载看看,这里是纯题目,你下载了再来私信我答案。按数据结构教材分章节,每一章节都有选择题、或有判断题、填空题、算法设计题及应用题,题型丰富多样,共五种类型题目。本学期已过去一半,相信你数据结构叶已经学得差不多了,是时候拿题来练练手了,如果你考研,更需要这份1800道题来巩固自己的基础及攻克重点难点。现在下载,不早不晚,越往后拖,越到后面,你身边的人就越卷,甚至卷得达到你无法想象的程度。我也是曾经遇到过这样的人,学习,练题,就要趁现在,不然到时你都不知道要刷数据结构题好还是高数、工数、大英,或是算法题?学完理论要及时巩固知识内容才是王道!记住!!!下载了来要答案(v:zywcv1220)。

特邀编辑特刊:安全可信计算

10特刊客座编辑安全和可信任计算0OZGUR SINANOGLU,阿布扎比纽约大学,阿联酋 RAMESHKARRI,纽约大学,纽约0人们越来越关注支撑现代社会所有信息系统的硬件的可信任性和可靠性。对于包括金融、医疗、交通和能源在内的所有关键基础设施,可信任和可靠的半导体供应链、硬件组件和平台至关重要。传统上,保护所有关键基础设施的信息系统,特别是确保信息的真实性、完整性和机密性,是使用在被认为是可信任和可靠的硬件平台上运行的软件实现的安全协议。0然而,这一假设不再成立;越来越多的攻击是0有关硬件可信任根的报告正在https://isis.poly.edu/esc/2014/index.html上进行。自2008年以来,纽约大学一直组织年度嵌入式安全挑战赛(ESC)以展示基于硬件的攻击对信息系统的容易性和可行性。作为这一年度活动的一部分,ESC2014要求硬件安全和新兴技术�

如何查看mysql版本

### 回答1: 可以通过以下两种方式来查看MySQL版本: 1. 通过命令行方式: 打开终端,输入以下命令: ``` mysql -V ``` 回车后,会显示MySQL版本信息。 2. 通过MySQL客户端方式: 登录到MySQL客户端,输入以下命令: ``` SELECT VERSION(); ``` 回车后,会显示MySQL版本信息。 ### 回答2: 要查看MySQL的版本,可以通过以下几种方法: 1. 使用MySQL命令行客户端:打开命令行终端,输入mysql -V命令,回车后会显示MySQL的版本信息。 2. 使用MySQL Workbench:打开MyS

TFT屏幕-ILI9486数据手册带命令标签版.pdf

ILI9486手册 官方手册 ILI9486 is a 262,144-color single-chip SoC driver for a-Si TFT liquid crystal display with resolution of 320RGBx480 dots, comprising a 960-channel source driver, a 480-channel gate driver, 345,600bytes GRAM for graphic data of 320RGBx480 dots, and power supply circuit. The ILI9486 supports parallel CPU 8-/9-/16-/18-bit data bus interface and 3-/4-line serial peripheral interfaces (SPI). The ILI9486 is also compliant with RGB (16-/18-bit) data bus for video image display. For high speed serial interface, the ILI9486 also provides one data and clock lane and supports up to 500Mbps on MIPI DSI link. And also support MDDI interface.

特邀编辑导言:片上学习的硬件与算法

300主编介绍:芯片上学习的硬件和算法0YU CAO,亚利桑那州立大学XINLI,卡内基梅隆大学TAEMINKIM,英特尔SUYOG GUPTA,谷歌0近年来,机器学习和神经计算算法取得了重大进展,在各种任务中实现了接近甚至优于人类水平的准确率,如基于图像的搜索、多类别分类和场景分析。然而,大多数方法在很大程度上依赖于大型数据集的可用性和耗时的离线训练以生成准确的模型,这在许多处理大规模和流式数据的应用中是主要限制因素,如工业互联网、自动驾驶车辆和个性化医疗分析。此外,这些智能算法的计算复杂性仍然对最先进的计算平台构成挑战,特别是当所需的应用受到功耗低、吞吐量高、延迟小等要求的严格限制时。由于高容量、高维度和高速度数据,最近传感器技术的进步进一步加剧了这种情况。0在严格的条件下支持芯片上学习和分类的挑战0性�