您的位置:首页 > 编程语言 > Python开发

python之路-------字符串与正则表达式

2015-08-14 16:45 901 查看
1.1、#####去掉字符串中的转义符string.strip()

print "hello\tworld\n"

>>> word="\thello world\n"

>>> 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']
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: