zoukankan      html  css  js  c++  java
  • python之路-------字符串与正則表達式

    1.1、#####去掉字符串中的转义符string.strip()
    print "hello world "
    >>> word=" hello world "
    >>> print "word.strip()后输出:",word.strip()
    word.strip()后输出: hello world
    >>> print "word.lstrip()后输出:",word.lstrip()
    word.lstrip()后输出: hello world

    >>>


    1.2、#####字符串拼接使用+或者join
    >>> str1="you "
    >>> str2="and "
    >>> str3="me"
    >>> 
    >>> 
    >>> result=str1+str2+str3
    >>> print result
    you and me
    ##python提供了函数join()连接字符串,join()配合列表实现多个字符串的连接十分方便
    >>> strs=['you ','and ','me']
    >>> "".join(strs)

    'you and me'


    1.3、#####字符串的截取,[start:end:step]从string的第start索引位置開始到第end个索引之间(不包含end)截取子串,截取的步长是step
    ##获取偶数位的字符
    >>> str1="hello world"
    >>> print str1[1::2]
    el ol
    >>> print str1[1:3:2]
    e

    ##split()使用
    split([char][,num]):參数char表示用于分隔的字符,默认的分隔符是空格;參数num表示分隔的次数,num+1个子串
    >>> str1="yangsq said: 1, 2, 3, 4"
    >>> str1.split()
    ['yangsq', 'said:', '1,', '2,', '3,', '4']
    >>> str1.split(',',2)

    ['yangsq said: 1', ' 2', ' 3, 4']


    1.4、#####字符串的比較
    java使用equal()比較两个字符串的内容。python直接使用==、!=比較字符串的内容,假设比較的两个变量的类型不同。比較的内容自然不同
    >>> str1,str2=1,"1"
    >>> if str1!=str2:print "不同样"
    ... 
    不同样
    >>> if str(str1)==str2:print "同样"
    ... 
    同样


    ##比較字符串,能够比較截取的子串,也能够比較字符串的开头和结尾部分(使用startswith或endswith()函数)


    1.5、#####字符串的反转
    def reverse():
    l1=list("hello")
    out=''
    for i in range(len(l1),0,-1):
    print("l1[i-1]->",l1[i-1])
    out+=l1[i-1]
    else:
    print "out=%s" %out
    print type(out)
    >>> reverse()
    ('l1[i-1]->', 'o')
    ('l1[i-1]->', 'l')
    ('l1[i-1]->', 'l')
    ('l1[i-1]->', 'e')
    ('l1[i-1]->', 'h')
    olleh

    python的列表是对字符串进行处理的经常使用方式。灵活使用列表等内置数据结构处理字符串。可以减少编程的复杂度。利用序列的"切片"实现字符串的反转最为简洁
    >>> str1="hello"
    >>> str1[::-1]

    'olleh'


    1.6、字符串的查找和替换
    help(str.find):find(...) S.find(sub [,start [,end]]) -> int,假设找到字符串sub。则返回源字符串第1次出现的索引。否则返回-1
    help(str.replace):replace(...) S.replace(old, new[, count]) -> string。replace实现字符串的替换。该函数能够指定替换的次数,默认是替换全部匹配的old
    >>> str1="hello world,hello china"
    >>> print str1.replace("hello","hi")
    hi world,hi china
    >>> print str1.replace("hello","hi",1)

    hi world,hello china


    1.7、字符串与日期的转换
    time.strftime(format[,tuple]):将时间转换为字符串。format表示格式化日期的字符;tuple表示须要转换的时间,用元组存储
    >>> import time
    >>> print strftime("%Y-%m-%d",time.localtime()) ##报错了
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'strftime' is not defined
    >>> print time.strftime("%Y-%m-%d",time.localtime())
    2015-08-14
    >>> print time.strftime("%Y-%m-%d %X",time.localtime())
    2015-08-14 16:42:43

    time.strptime(string,format):将字符串转换为时间,函数返回一个存放时间的元组


    1.8、正則表達式

    默认情况下,正則表達式将匹配最长的字符串作为结果。能够通过在限定符后面加入?的方式。获取最短匹配的结果。

    比如:对abcabcab进行a*c匹配,则结果为abcabc;
    对abcabcab进行a*?c匹配,则结果为返回2个abc;

    ^与[^m]中^的含义不同,前者是以xxx開始,后者表示除了......;(和)是正則表達式中特殊的字符,假设要把它们作为普通字符处理。须要在它们前面加入转义字符


    1.9、使用re模块处理正则笔表达式
    re模块提供了一些正則表達式进行查找、替换、分隔字符串的函数。re模块的经常使用函数有:findall(pattern, string, flags=0)、sub(pattern, repl, string, count=0)、match(pattern, string, flags=0)比如:re.findall('%s(d{1,20})' % i,result)。
    函数match()必须从字符串的第0个索引位置開始搜索。假设从第0个索引位置的字符就不匹配,match()的匹配就会失败。


    re模块的一部分函数中都有一个flags參数,该參数用于设置匹配的附加选项;

    >>> print re.sub("hello","hi",s)
    hi world
    >>> print re.sub("world","hi",s[-4:])
    orld
    >>> print re.sub("world","china",s[-5:])
    china

    注意:正則表達式的解析很耗时,假设多次使用同一规则匹配字符串,能够使用compile()函数进行预编译。compile返回一个pattern对象。该对象拥有一系列方法用于查找、替换或扩展
    字符串。
    >>> import re
    >>> s="1abc23def45"
    >>> print re.findall("d+",s)
    ['1', '23', '45']
    >>> p=re.compile("d+")
    >>> print p.findall(s)
    ['1', '23', '45']


  • 相关阅读:
    C语言考点例题解析
    五笔打字
    常用快捷键
    网络基础知识
    人口增长
    8 封装
    9 绑定方法和非绑定方法
    6 抽象类
    7 多态和多态性
    5 组合
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7064542.html
Copyright © 2011-2022 走看看