国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 兴义市| 太谷县| 商水县| 鄂托克前旗| 河西区| 土默特右旗| 新乐市| 隆昌县| 清苑县| 青冈县| 霍州市| 绵阳市| 印江| 永州市| 盐津县| 蒲江县| 东明县| 黑龙江省| 平阴县| 高阳县| 南陵县| 迁安市| 祁阳县| 望谟县| 河源市| 昌吉市| 田林县| 石泉县| 德令哈市| 专栏| 全椒县| 武城县| 商城县| 平原县| 车致| 梁山县| 合川市| 平定县| 莱芜市| 凌源市| 迁西县|