//test.py

1 import re

2

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

4 print (re.search('com', 'www.myweb.com').span())

5

6 line = 'Cats are smarter than dogs'

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

8 #matchObj = re.search(r'(.*) are (.*?) .*', line, re.M|re.I)

9 matchObj = re.search(r'(.*) are (.*) .*', line, re.M|re.I)

10 if matchObj:

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

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

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

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

15 else:

16         print 'No match'

17

18 phone = '2017-989-898' # this is one foreign phone number

19 num = re.sub(r'#.*$', '', phone)

20 print 'phone number: ',num

21 num = re.sub(r'\D', '', phone)

22 print "phone number: ", num

23

24 def double(matched):

25         value = int(matched.group('value'))

26         return str(value*2)

27 s = 'A23G4HFD567'

28 print re.sub(r'(?P<value>\d+)', double, s)

//result

# python test.py
(0, 3)
(10, 13)
matchObj.group() Cats are smarter than dogs
matchObj.group(1) Cats
matchObj.group(2) smarter than
matchObj.groups() ('Cats', 'smarter than')
phone number: 2017-989-898
phone number: 2017989898
A46G8HFD1134

Finally:

稍微解释一下:

search 搜索匹配不是必须从字符串开始处匹配,任意位置匹配

span方法是转化匹配对象为原搜索字符串中的位置信息

r'(.*) are (.*) .*'正则表达式的含义:任意字符重复0次到多次,遇到 are,再次任意字符重复0-多次,直至遇到一个任意字符重复0-多次,显然最后的dogs之前会有smart than,如果是条件r'(.*) are (.*?) .*', dog之前重复0-1次,那么就是smart

替换的正则表达,r'#.*$',从#号开始到结尾,任意重复0-多次

r'\D',匹配非数字

r'(?P<value>\d+)',匹配数字1-多次,组取名字value,以double函数做替换项(字符串-》数字-》2倍-》字符串),匹配替换

其实,这个东西,你要用得着时,还是真有用啊!!!

04-27 04:04