zoukankan      html  css  js  c++  java
  • python 正则表达式(一)

    正则表达式(regular exprssion)是一种形式化语法描述的文本匹配模式。模式被解释为一组指令,然后会执行这组指令。以一个字符串作为输入,生成一个匹配的子集或源字符串的修改版本。

    表达式可以包括字面量文本匹配、重复、模式组合、分支一级其他复杂的规则。

    • 查找文本中的模式

    re最常见的用法就是搜索文本中的模式。search()函数取模式和要草庙的文本作为输入,若找到这个模式则返回一个Match对象。若未找到,返回None。

    import re
    
    parttern = 'this'
    text = 'Does this text match the pattern?'
    
    match = re.search(parttern, text)
    
    s = match.start()
    e = match.end()
    
    print 'Found "%s"
    in "%s"
    from %d to %d ("%s")' % 
          (match.group(), match.string, s, e, text[s:e])

    group() 显示被匹配的字符串

    start() end()可以给出字符串中的相应索引,指示与模式匹配的文本在字符串中出现的位置。

    Found "this"
    in "Does this text match the pattern?"
    from 5 to 9 ("this")

    还可以使用 span() 函数返回被匹配的字符串的位置(match.span())

  • 相关阅读:
    Hive_元数据配置到MySQL
    第一篇
    mysql查询结果添加序列号
    java中正则表达式
    java位运算
    正数负数原码,反码,补码
    各进制间相互转换
    linux下默认安装jdk路径查找
    localhost:8080/manager/status无法访问403 Access Denied
    Idea官网指南
  • 原文地址:https://www.cnblogs.com/roicel/p/3160008.html
Copyright © 2011-2022 走看看