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. 多线程

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

    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)

猜你喜欢

转载自blog.csdn.net/qq_42650983/article/details/81392582
今日推荐