29 lines
784 B
Python
29 lines
784 B
Python
# -*- coding: UTF-8 -*-
|
|
|
|
import re
|
|
print(re.match('www', 'www.runoob.com').span()) # 在起始位置匹配
|
|
print(re.match('com', 'www.runoob.com')) # 不在起始位置匹配
|
|
|
|
line = "Cats are smarter than dogs";
|
|
|
|
matchObj = re.match(r'dogs', line, re.M|re.I)
|
|
if matchObj:
|
|
print("match --> matchObj.group() : ", matchObj.group())
|
|
else:
|
|
print("No match!!")
|
|
|
|
matchObj = re.search(r'dogs', line, re.M|re.I)
|
|
if matchObj:
|
|
print("search --> searchObj.group() : ", matchObj.group())
|
|
else:
|
|
print("No match!!")
|
|
|
|
phone = "2004-959-559 # 这是一个国外电话号码"
|
|
|
|
# 删除字符串中的 Python注释
|
|
num = re.sub(r'#.*$', "", phone)
|
|
print("电话号码是: ", num)
|
|
|
|
# 删除非数字(-)的字符串
|
|
num = re.sub(r'\D', "", phone)
|
|
print("电话号码是 : ", num) |