zoukankan      html  css  js  c++  java
  • python练习:正则表达式

    import re
    #过滤出中国,其中name不一定总为name
    str = '<div class ="name">中国</div>'
    resu = re.findall(r'<div class =".*">(.*?)</div>',str)
    print(resu)
    
    #过滤中文:汉字整体打印
    str1 = "not 404 6.775 found   魏无羡  888  蓝湛"
    print ("
    练习:过滤出“",str1,"”中的中文,汉字整体打印")
    list= str1.split(" ")               #['not', '404', '6.775', 'found', '', '', '魏无羡', '', '888', '', '蓝湛']
    pattern = 'd+.?d*|[a-zA-Z]+'     #d+.?d*  过滤整数、小数等,[a-zA-Z]  过滤字母
    resu1 = re.findall(pattern,str1)    #['not', '404', '6.775', 'found', '888']
    for i in resu1:
        list.remove(i)                  #list:['', '', '魏无羡', '', '', '蓝湛']
    while '' in list:
        list.remove('')                 #去掉列表中的空格,不去也可以的,list:['魏无羡', '蓝湛']
    str1_resu = ' '.join(list)
    print (str1_resu)
    
    
    #过滤中文2:单个汉字打印
    str1 = "not 404 6.775 found  魏无羡  888  蓝湛"
    print ("
    练习2:过滤出",str1,"中的中文,单个汉字打印")
    list= str1.split(" ")
    print (list)
    pattern = '[u4e00-u9fa5]'     #[u4e00-u9fa5]  过滤汉字
    resu1 = re.findall(pattern,str1)
    print (resu1)

    结果:

    ['中国']
    
    练习:过滤出“ not 404 6.775 found   魏无羡  888  蓝湛 ”中的中文,汉字整体打印
    魏无羡 蓝湛
    
    练习2:过滤出 not 404 6.775 found  魏无羡  888  蓝湛 中的中文,单个汉字打印
    ['not', '404', '6.775', 'found', '', '魏无羡', '', '888', '', '蓝湛']
    ['', '', '', '', '']
    
    Process finished with exit code 0
  • 相关阅读:
    swift 一些公共的宏重新定义
    iOS中正则表达式的使用
    失败的尝试,使用继承扩展数组,以及ES6的必要性
    用travis-ci编译android版nodejs
    用es6写一个分数库
    快速平方根算法的javascript实现
    windows中vim以及cmder的使用
    nodejs的交叉(跨平台)编译(to android)
    用链表实现nodejs的内存对象管理
    给socks-proxy-agent增加认证
  • 原文地址:https://www.cnblogs.com/jxba/p/11842386.html
Copyright © 2011-2022 走看看