zoukankan      html  css  js  c++  java
  • python死磕七之字符串与文本

      一、你想在字符串中搜索和匹配指定的文本模式

      遗漏点:re模块其实也是帮助我们进行字符串处理的重要工具,我之前总是想着用内建的函数来处理,其实如果是复杂的文本和数据结构,re模块能帮助我们处理很多信息。

      对于简单的字面模式,直接使用 str.replace() 方法即可,比如:

    >>> text = 'yeah, but no, but yeah, but no, but yeah'
    >>> text.replace('yeah', 'yep')
    'yep, but no, but yep, but no, but yep'
    >>>

      对于复杂的模式,请使用 re 模块中的 sub() 函数。 为了说明这个,假设你想将形式为 11/27/2012 的日期字符串改成 2012-11-27 。示例如下:

    >>> text = 'Today is 11/27/2012. PyCon starts 3/13/2013.'
    >>> import re
    >>> re.sub(r'(d+)/(d+)/(d+)', r'3-1-2', text)
    'Today is 2012-11-27. PyCon starts 2013-3-13.'

      二、你需要以忽略大小写的方式搜索与替换文本字符串

      为了在文本操作时忽略大小写,你需要在使用 re 模块的时候给这些操作提供 re.IGNORECASE 标志参数。比如:

    >>> text = 'UPPER PYTHON, lower python, Mixed Python'
    >>> re.findall('python', text, flags=re.IGNORECASE)
    ['PYTHON', 'python', 'Python']
    >>> re.sub('python', 'snake', text, flags=re.IGNORECASE)
    'UPPER snake, lower snake, Mixed snake'

      最后的那个例子揭示了一个小缺陷,替换字符串并不会自动跟被匹配字符串的大小写保持一致。 为了修复这个,你可能需要一个辅助函数,就像下面的这样:

    def matchcase(word):
        def replace(m):
            text = m.group()
            if text.isupper():
                return word.upper()
            elif text.islower():
                return word.lower()
            elif text[0].isupper():
                return word.capitalize()
            else:
                return word
        return replace
    >>> re.sub('python', matchcase('snake'), text, flags=re.IGNORECASE)       
    'UPPER SNAKE, lower snake, Mixed Snake'

    matchcase('snake') 返回了一个回调函数(参数必须是 match 对象),sub() 函数除了接受替换字符串外,还能接受一个回调函数。

      三、你正在试着使用正则表达式去匹配一大块的文本,而你需要跨越多行去匹配。

    >>> comment = re.compile(r'/*(.*?)*/')
    >>> text1 = '/* this is a comment */'
    >>> text2 = '''/* this is a
    ... multiline comment */
    ... '''
    >>>
    >>> comment.findall(text1)
    [' this is a comment ']
    >>> comment.findall(text2)

      re.compile() 函数接受一个标志参数叫 re.DOTALL ,在这里非常有用。 它可以让正则表达式中的点(.)匹配包括换行符在内的任意字符。比如:

    >>> comment = re.compile(r'/*(.*?)*/', re.DOTALL)
    >>> comment.findall(text2)
    [' this is a
     multiline comment ']

      

      四、你想通过某种对齐方式来格式化字符串

      于基本的字符串对齐操作,可以使用字符串的 ljust() , rjust() 和 center() 方法。比如:

    >>> text = 'Hello World'
    >>> text.ljust(20)
    'Hello World         '
    >>> text.rjust(20)
    '         Hello World'
    >>> text.center(20)
    '    Hello World     '
    >>> text.rjust(20,'=')
    '=========Hello World'
    >>> text.center(20,'*')
    '****Hello World*****'
    >>>

      函数 format() 同样可以用来很容易的对齐字符串。 你要做的就是使用 <,> 或者 ^ 字符后面紧跟一个指定的宽度。比如:

    >>> format(text, '>20')
    '         Hello World'
    >>> format(text, '<20')
    'Hello World         '
    >>> format(text, '^20')
    '    Hello World     '
    >>>

      如果你想指定一个非空格的填充字符,将它写到对齐字符的前面即可:

    >>> format(text, '=>20s')
    '=========Hello World'
    >>> format(text, '*^20s')
    '****Hello World*****'
    >>>

      当格式化多个值的时候,这些格式代码也可以被用在 format() 方法中。比如:

    >>> x = 1.2345
    >>> format(x, '>10')
    '    1.2345'
    >>> format(x, '^10.2f')
    '   1.23   '
    >>>
  • 相关阅读:
    Android视频播放软解与硬解的区别
    Android ViewPager嵌套ViewPager滑动冲突处理方法
    2.2 Consumer API官网剖析(博主推荐)
    2.1 Producer API官网剖析(博主推荐)
    2. APIS官网剖析(博主推荐)
    1.5 Upgrading From Previous Versions官网剖析(博主推荐)
    1.4 Ecosystem官网剖析(博主推荐)
    1.3 Quick Start中 Step 8: Use Kafka Streams to process data官网剖析(博主推荐)
    1.3 Quick Start中 Step 7: Use Kafka Connect to import/export data官网剖析(博主推荐)
    1.3 Quick Start中 Step 6: Setting up a multi-broker cluster官网剖析(博主推荐)
  • 原文地址:https://www.cnblogs.com/jimmyhe/p/10821763.html
Copyright © 2011-2022 走看看