split:切割
import re s='2018-08-03' regex=r'-' res=re.split(regex,s) print(res) #输出['2018','08','03']
match:从字符串第一个开始匹配
import re s='hello world ada' regex=r'\w{5}' reg=re.compile(regex) res=reg.match(s) print(res) #匹配结果:'hello'
search:全字符串匹配
import re s='ff hello world' regex='\w{5}' reg=re.compile(regex) res=reg.search(s) print(res) #匹配结果'hello'
findall:找到所有符合要求的字符串
s='Hello world kdk' regex=r'\w{5}' reg=re.compile(regex,flags=re.I)#re.I不区分大小写 res=reg.findall(s) print(res) #匹配结果['hello','world']
?P<>设置变量
str='abcabc is a abc word,and defdef is a word too' //设置abc为变量tt,下式匹配到'abcabc'字符串 res=re.findall('((?P<tt>abc){2})',str)
多线程
import threading import time def action(n): time.sleep(1) print(threading.current_thread.getName()) if __name__=='__main__': thread_list=[] for i in range(10): #构建一个线程对象,target执行线程的方法 t=threading.Thread(target=action,args=(i,)) t=setDaemon(True)#后台进程,默认为False前台进程 thread_list.append(t) t.start()#启动线程 for th in thread_list: th.join()#阻塞上下文的线程 print('开始线程')
自定义多线程
import threading class Mythread(threading.Thread): def __init__(self,arg): super(MyThread,self).__init() self.arg=arg def run(self):#定义每个线程要执行的函数 time.sleep(1) print(self.arg)
多线程锁,线程同步
import threading import time count=0 lock=threading.Rlock()#循环锁,不会死锁 lock=threading.Lock()#系统锁,会死锁 def action(arg): lock.acquire()#获取钥匙 time.sleep(1) global count count+=1 print(threading.current_thread()) count-=1 print(count) lock.release()#释放钥匙 th=[] for i in range(4): th.append(i) i=threading.Thread(target=action,args=(i,)) t.start() for t in th: t.join()
python正则表达式和、多线程
最新推荐文章于 2022-08-13 10:41:22 发布