python正则表达式和、多线程

  1. split:切割

    import re
    s='2018-08-03'
    regex=r'-'
    res=re.split(regex,s)
    print(res)
    
    #输出['2018','08','03']
    
  2. match:从字符串第一个开始匹配

    import re
    s='hello world ada'
    regex=r'\w{5}'
    reg=re.compile(regex)
    res=reg.match(s)
    print(res)
    
    #匹配结果:'hello'
    
  3. search:全字符串匹配

    import re
    s='ff hello world'
    regex='\w{5}'
    reg=re.compile(regex)
    res=reg.search(s)
    print(res)
    
    #匹配结果'hello'
    
  4. 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']
    
  5. ?P<>设置变量

    str='abcabc is a abc word,and defdef is a word too'
    //设置abc为变量tt,下式匹配到'abcabc'字符串
    res=re.findall('((?P<tt>abc){2})',str)
  6. 多线程

    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('开始线程')
  7. 自定义多线程

    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)
  8. 多线程锁,线程同步

    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()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值