zoukankan      html  css  js  c++  java
  • Python正则表达

    import re
    pattern = re.compile('hello')
    match = pattern.match("hello world")
    print (match)
    
    
    word = "http://www.ichunqiu.com/1.1.1" 
    
    print(re.findall('.',word))      # . 是匹配除/n以外的字符
    print(re.findall('.',word))     #  是匹配需要转义的字符
    print(re.findall('[^.://]',word))# [] 对应位置可以是任意字符 如[^abc]表示不是abc的字符
    print(re.findall('d.',word))     # d 匹配数字
    print(re.findall('D',word))     # D 匹配非数字
    print(re.findall('s',word))     # s 匹配空格
    print(re.findall('S',word))     # S 匹配非空字符
    print(re.findall('w',word))     # w 匹配单词字符
    print(re.findall('W',word))     # W 匹配非单词字符 如 :、/、。
    print(re.findall('http*',word))     # * 匹配前1个字符0次或者无数次,+ 1次或者无限次 ?0次或者一次
    print(re.findall('http{2}','httphttp')) # {m}匹配前一个字符m次,{m,n}匹配到n-m次
    print(re.findall('abc|fgh','abcfgh'))  # | 先匹配|左边,没有就匹配右边
    print(re.findall('http://(.*?)/1.1.1',word)) # ()将()作为一个分组,从表达式的左边开始遇到(就+1

    运行结果:

  • 相关阅读:
    error C2144
    Linux下STL使用
    GZip压缩的实例
    头文件类型的选择
    双重指针
    locate命令的使用
    有了malloc/free为什么还要new/delete !
    Mongdb windows下安装
    虚函数工作机制
    extern使用
  • 原文地址:https://www.cnblogs.com/jiluxuexi/p/12357287.html
Copyright © 2011-2022 走看看