1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
import re
m = re.match(r'www\.(.*)\..{3}','www.python.org') test_1 = m.group() print('m is :',m,'\n','m.group() is :',test_1) test_2 = m.group(1) print('m is :',m,'\n','m.group(1) is :',test_2)
test_3 = m.start(1) print('m.start is :',test_3)
test_4 = m.end(1) print('m.end is :',test_4)
test_5 = m.span(1) print('m.span is :',test_5)
emphasis_pattern = r'\*([^*]+)\*' test_6 = re.sub(emphasis_pattern,r'<em>\1</em>','Hello *world*!') print('re.sub is :',test_6)
emphasis_pattern = r'\*(.+)\*' test_7 = re.sub(emphasis_pattern,r'<em>\1</em>','*this* is *it*! ') print('重复运算符 默认贪婪模式 :',test_7)
emphasis_pattern = r'\*(.+?)\*' test_8 = re.sub(emphasis_pattern,r'<em>\1</em>','*this* is *it*! ') print('重复运算符+? 改为非贪婪模式 :',test_8)
|