zoukankan      html  css  js  c++  java
  • Python实战之正则表达式RE/re学习笔记及简单练习

    # .,w,s,d,,^,$
    # *,+,?,{n},{n,},{n,m}

    # re模块用于对python的正则表达式的操作。
    #
    # 字符:
    #
    #   . 匹配除换行符以外的任意字符
    #   w 匹配字母或数字或下划线或汉字
    #   s 匹配任意的空白符
    #   d 匹配数字
    #    匹配单词的开始或结束
    #   ^ 匹配字符串的开始
    #   $ 匹配字符串的结束
    #
    # 次数:
    #
    #   * 重复零次或更多次
    #   + 重复一次或更多次
    #   ? 重复零次或一次
    #   {n} 重复n次
    #   {n,} 重复n次或更多次
    #   {n,m} 重复n到m次


    import re
    # match(pattern, string, flags=0)
    # 从起始位置开始根据模型去字符串中匹配指定内容,匹配单个
    # 正则表达式
    # 要匹配的字符串
    # 标志位,用于控制正则表达式的匹配方式
    # strings = re.match('d+','123df45')
    # print(strings)
    # print(strings.group())
    # print(strings.groups())
    # <_sre.SRE_Match object; span=(0, 3), match='123'>
    # 123
    # ()



    #根据模型去字符串中匹配指定内容,匹配单个
    # obj = re.search('d+','u9f34k')
    # print(obj)
    # print(obj.group())
    # <_sre.SRE_Match object; span=(1, 2), match='9'>
    # 9




    # group和groups
    # a = '123abc456'
    # print(re.search("([0-9]*)([a-z]*)([0-9]*)",a).group())
    # # 123abc456
    # print(re.search("([0-9]*)([a-z]*)([0-9]*)",a).groups())
    # # ('123', 'abc', '456')
    # print(re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(0))
    # # 123abc456
    # print(re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(1))
    # # 123
    # print(re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(2))
    # #abc
    # print(re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(3))
    # #456


    # findall(pattern, string, flags=0)
    # 上述两中方式均用于匹配单值,即:只能匹配字符串中的一个,如果想要匹配到字符串中所有符合条件的元素,则需要使用 findall。
    # a = '123abc567'
    # obj = re.findall('d+',a)
    # print(obj)
    # print()
    # ['123', '567']


    # sub(pattern, repl, string, count=0, flags=0)
    # 用于替换匹配的字符串

    # content = '123abc456'
    # new_content = re.sub('d+','sb',content)
    # # sbabcsb
    # print(new_content)
    # new = re.sub('d+','xxoo',content,3)
    # print(new)
    # xxooabcxxoo

    content = "'1 - 2 * ((60-30+1*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2) )'"
    # new_content = re.split('*',content)
    # print(new_content)
    # # ["'1 - 2 ", ' ((60-30+1', '(9-2', '5/3+7/3', '99/4', '2998+10', '568/14))-(-4', '3)/(16-3', "2) )'"]
    # new_content2 = re.split('*',content,1)
    # print(new_content2)
    # # ["'1 - 2 ", " ((60-30+1*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2) )'"]
    #
    # new_content3 = re.split('*',content,3)
    # print(new_content3)
    # # ["'1 - 2 ", ' ((60-30+1', '(9-2', "5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2) )'"]



    # new_content = re.split('[+-*/]+',content)
    # print(new_content)
    # ["'1 ", ' 2 ', ' ((60', '30', '1', '(9', '2', '5', '3', '7', '3', '99', '4', '2998', '10', '568', '14))', '(', '4', '3)', '(16', '3', "2) )'"]


    # inpp = '1-2*((60-30 +(-40-5)*(9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2))'
    # inpp = re.sub('s*','',inpp)
    # print(inpp)
    # # 1-2*((60-30+(-40-5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))
    #
    # new_content = re.split('(([+-*/]?d+[+-*/]?d+){1})',inpp, 1)
    # print(new_content)
  • 相关阅读:
    Hibernate核心接口和类
    Struts 2基础知识
    博客开篇
    程序员的幽默笑话(深意爆笑)
    追MM的各种算法
    全局css(慕课)
    全局css
    目录的分层
    class 设置多个css属性
    Python爬虫学习:四、headers和data的获取
  • 原文地址:https://www.cnblogs.com/william126/p/7123529.html
Copyright © 2011-2022 走看看