python regularexpress

这个正则表达式,我还真没有接触过,在python首次学习

//test.py

1 import re

  2

  3 print (re.match('www', 'www.myweb.com').span())

  4 print (re.match('com', 'www.myweb.com'))

  5

  6 line = 'Cats are smarter than dogs'

  7 matchObj = re.match(r'(.*) are (.*?) (.*?) (.+)', line, re.M|re.I)

  8 if matchObj:

  9         print 'matchObj.group()', matchObj.group()

 10         print 'matchObj.group(1)', matchObj.group(1)

 11         print 'matchObj.group(2)', matchObj.group(2)

 12         print 'matchObj.groups()', matchObj.groups()

 13 else:

 14         print 'No match'

//result

# python test.py
(0, 3)
None
matchObj.group() Cats are smarter than dogs
matchObj.group(1) Cats
matchObj.group(2) smarter
matchObj.groups() ('Cats', 'smarter', 'than', 'dogs')

Finally:

做完才发现,以前也弄过,不过都是自己写代码匹配而已

但是,既然有现成的匹配模块了,为何还自己写呢?用吧,大不了自己多学点儿匹配的规则而已

肯定人家的匹配规则多多啦

原文地址:https://www.cnblogs.com/woodzcl/p/7810743.html