第4章-字符串与正则表达式.pptVIP

  • 18
  • 0
  • 约2.02万字
  • 约 60页
  • 2019-02-20 发布于江苏
  • 举报
4.2.4 使用正则表达式对象 example = Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts.‘ pattern = pile(r\bb\w*\b,re.I) print pattern.sub(*,example) * is * than ugly. Explicit is * than implicit. Simple is * than complex. Complex is * than complicated. Flat is * than nested. Sparse is * than dense. Readability counts. 4.2.4 使用正则表达式对象 print pattern.sub(*,example,1) * is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. pattern = pile(r\bb\w*\b) print pattern.sub(*,example,1) Beautiful is * than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. 4.2.4 使用正则表达式对象 分割字符串: split(string[, maxsplit = 0]) -- list 4.2.4 使用正则表达式对象 example = rone,two,three.four/file\six?seven[eight]nine|ten‘ pattern = pile(r[,./\\?[\]\|]) pattern.split(example) [one, two, three, four, file, six, seven, eight, nine, ten] example = rone1two2three3four4file5six6seven7eight8nine9ten pattern = pile(r\d+) pattern.split(example) [one, two, three, four, file, six, seven, eight, nine, ten] example = rone two three four,file.six.seven,eight,nine9ten pattern = pile(r[\s,.\d]+) pattern.split(example) [one, two, three, four, file, six, seven, eight, nine, ten] 4.2.5 子模式与match对象 使用()表示一个子模式,即()内的内容作为一个整体出现,例如’(red)+’可以匹配’redred’、’redredred‘等多个重复’red’的情况。 telNumber = Suppose my Phone No. is 0535-1234567, yours is 010 his is 025 pattern = pile(r(\d{3,4})-(\d{7,8})) pattern.findall(telNumber) [(0535, 1234567), (010,, (025,] 4.2.5 子模式与match对象 正则表达式对象的match方法和search方法匹配成功后返回match对象。 match对象的主要方法有group()、groups()、groupdict()、start()、e

文档评论(0)

1亿VIP精品文档

相关文档