前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 字符串搜索

python 字符串搜索

作者头像
用户5760343
发布2022-01-10 08:45:21
5990
发布2022-01-10 08:45:21
举报
文章被收录于专栏:sktjsktj

text = 'yeah, but no, but yeah, but no, but yeah' Search for the location of the first occurrence text.find('no') 10 text = 'Today is 11/27/2012. PyCon starts 3/13/2013.' datepat.findall(text) ['11/27/2012', '3/13/2013']

或者正则

text1 = '11/27/2012' text2 = 'Nov 27, 2012' import re Simple matching: \d+ means match one or more digits if re.match(r'\d+/\d+/\d+', text1): ... print('yes') ... else: ... print('no') ... yes if re.match(r'\d+/\d+/\d+', text2): ... print('yes') ... else: ... print('no') ... no

datepat = re.compile(r'(\d+)/(\d+)/(\d+)') m = datepat.match('11/27/2012') m <_sre.SRE_Match object at 0x1005d2750> Extract the contents of each group m.group(0) '11/27/2012' m.group(1) '11' m.group(2) '27' m.group(3) '2012' m.groups() ('11', '27', '2012') month, day, year = m.groups() Find all matches (notice splitting into tuples) text 'Today is 11/27/2012. PyCon starts 3/13/2013.' datepat.findall(text) [('11', '27', '2012'), ('3', '13', '2013')] for month, day, year in datepat.findall(text): ... print('{}-{}-{}'.format(year, month, day)) ... 2012-11-27 2013-3-13

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.09.24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 或者正则
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档