一、正则表达式的使用

1. re.match()字符串的匹配
#正则表达式常用的3钟方法
#re.match()字符串的匹配问题
import re
pat="^[0-9]*$"  #数字字符串正则表达式
s="1233"
print("s是数字字符串吗?")
print(re.match(pat,s)  is not None)   #match表示匹配,当匹配不为空时,返回ture表示存在
pat="^[0-9]+.[0-9]{1,3}$" #查找保留1到3位小数正则表达式
s="23.344"
print("s是保留1到3位小数吗?")
print(re.match(pat,s) is not None)  
pat="^(\-)[0-9]+.[0-9]{1,3}$"#查找保留1到3位小数的负数
s="-3434334.34"
print("s是保留1到3位小数的负数吗?")
print(re.match(pat,s) is not None)
pat="^(\d{3,4}-)*\d{7,8}"  #查找前面是3-4位数字,后面是7-8位数字,中间用横线隔开的数
s="327-87943430"
print("s是前面是3-4位数字,后面是7-8位数字,中间用横线隔开的数吗?")
print(re.match(pat,s) is not None)
pat="^\d{3}-\d{8}|\d{4}-\d{7}$"#电话号码的正则表达式
s="027-87343430"
print("s是电话号码吗?")
print(re.match(pat,s) is not None)
pat="^\w+@(\w+\.)+\w+$" #查找邮箱 
s="nihao@173.com"
print("s是邮箱吗?")
print(re.match(pat,s) is not None)

python正则匹配以数字结尾 python正则匹配小数_笔记

2. re.findall()字符串的查找
import re
s="nihao@173.com 23423  nixiaolong@173.com"
pat="\w+@[\w.]+"
print("从字符串S1中找到的邮箱为")
print(re.findall(pat,s))

s1="我的电话号码是:155-23457869 和 155-231231213"
pat="\d{4}-\d{7}"
pat2="\d{3}-\d{8}"
print("从字符串S2中找到的电话为")
set1=set(re.findall(pat,s1)).union(re.findall(pat2,s1))  #连接两个字符串   利用set().union合并两个集合
print(set1)

python正则匹配以数字结尾 python正则匹配小数_理论课_02

3. re.split()字符串的分割
s="sdf  sd+s  sd;fr  h,t"
print(s.split())              #字符串的split方法只能有一种分隔符
print(re.split("[\s,;+]+",s)) #正则表达式的split方法可以有多个分隔符,[]里面的都是

python正则匹配以数字结尾 python正则匹配小数_笔记_03

4. 读取网站链接
import urllib.request
x=urllib.request.urlopen("http://www.douban.com")#对网页发出请求
b=x.read()  #读取网页的内容,此时读出来的是字节流
html=b.decode("UTF-8")  #将字节流转化为可显示中文的字符串形式
#print(html)   
pat="https://[\w./]+"  #对应链接的正则表达式
import re           
linkset=set((re.findall(pat,html))) #使用findall在网页字符串中查找对应的链接字符串(用set去重)
print(len(linkset))#查看豆瓣网一共有多少个链接

二、函数

1. 函数的定义
def f1(x):
    y=x+4
    return y
p=4
f1(p)

python正则匹配以数字结尾 python正则匹配小数_理论课_04

2. 形参的赋值
1.形参的赋值方式
def fun1(a,b,c):
    return a+(b*10)+(c*100)
print("形参自由赋值的结果为")
print(fun1(c=2,b=1,a=4))
print("形参默认赋值的结果为")
print(fun1(1,2,4))

python正则匹配以数字结尾 python正则匹配小数_笔记_05

2. 设定默认参数值
def fun1(a,b=3,c=5):  #在没有给形参b,c赋值的情况下,默认b=3,c=4
    return a+(b*10)+(c*100)
print("b,c采用默认值")
print(fun1(1)) #此时只需要给a赋值
print("b,c不采用默认值")
print(fun1(1,b=5,c=6))  #对应的被b,c明确指出进行修改

python正则匹配以数字结尾 python正则匹配小数_笔记_06

3. 形参和实参
1.局部变量无法影响全局变量
def f1():
    z=1000
    return z
z=10
print("调用f1()函数前的值z=",z)
f1()
print("调用f1()函数后的值z=",z)

python正则匹配以数字结尾 python正则匹配小数_python正则匹配以数字结尾_07

2.函数可以引用全局变量
def f1():
    print("函数内部引用全局变量值为")
    print(z)
    print("函数的返回值为")
    return 0
z=10
print(f1())

python正则匹配以数字结尾 python正则匹配小数_python正则匹配以数字结尾_08

3.使用global,在函数内部改变全局变量
def f1():
    global z
    z=100  
    return 

z=10
print("调用f1()函数前的值z=",z)
f1()
print("调用f1()函数后的值z=",z)

python正则匹配以数字结尾 python正则匹配小数_笔记_09

4. 斐波拉数列
#斐波拉数列
def fibo(n):
    if n==1:
        return 1
    if n==2:
        return 1
    x=fibo(n-1)+fibo(n-2)
    return x
fibo(3)
5. 定义一个时钟函数
#函数定义时间
def o2(s):             
    if len(s)==1:     #长度为1时,在前面加一个0(时钟都是两位数表示)
        return "0"+s
    else:
        return s


def getNowTime():
    import datetime
    x=datetime.datetime.today().timetuple()  #获取时间元组
    s1=o2(str(x[3]))+":"+o2(str(x[4]))+':'+o2(str(x[5]))   #str强制转换为字符串
    return s1
print("当前的时间为")
getNowTime()

python正则匹配以数字结尾 python正则匹配小数_正则表达式_10