国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python正則表達式re模塊詳解

2020-02-23 05:28:29
字體:
供稿:網(wǎng)友

快速入門

import repattern = 'this'text = 'Does this text match the pattern?'match = re.search(pattern, text)s = match.start()e = match.end()print('Found "{0}"/nin "{1}"'.format(match.re.pattern, match.string))print('from {0} to {1} ("{2}")'.format( s, e, text[s:e]))

執(zhí)行結果:

#python re_simple_match.py Found "this"in "Does this text match the pattern?"from 5 to 9 ("this")import re# Precompile the patternsregexes = [ re.compile(p) for p in ('this', 'that')]text = 'Does this text match the pattern?'print('Text: {0}/n'.format(text))for regex in regexes:  if regex.search(text):    result = 'match!'  else:    result = 'no match!'      print('Seeking "{0}" -> {1}'.format(regex.pattern, result))

執(zhí)行結果:

#python re_simple_compiled.py Text: Does this text match the pattern?Seeking "this" -> match!Seeking "that" -> no match!import retext = 'abbaaabbbbaaaaa'pattern = 'ab'for match in re.findall(pattern, text):  print('Found "{0}"'.format(match))

執(zhí)行結果:

#python re_findall.py Found "ab"Found "ab"import retext = 'abbaaabbbbaaaaa'pattern = 'ab'for match in re.finditer(pattern, text):  s = match.start()  e = match.end()  print('Found "{0}" at {1}:{2}'.format(text[s:e], s, e))

執(zhí)行結果:

#python re_finditer.py Found "ab" at 0:2Found "ab" at 5:7

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 山阳县| 西盟| 资中县| 定西市| 延川县| 于田县| 蒙自县| 开封市| 甘肃省| 永仁县| 铜山县| 衡南县| 肥城市| 洞口县| 台南县| 长春市| 武隆县| 察哈| 定州市| 阿拉尔市| 许昌市| 沙湾县| 紫云| 德惠市| 涞源县| 湘潭市| 从江县| 南木林县| 闸北区| 乌拉特前旗| 仁怀市| 客服| 林甸县| 原平市| 普安县| 军事| 军事| 古蔺县| 南宁市| 耒阳市| 全椒县|