2.字符串和文本
2.8编写多行模式的正则表达式
import re
comment = re.compile(r'/\*(.*?)\*/')
text1 = '/* this is a comment */'
#"""多行文本
text2 = '''/* this is a
multiline comment */
'''
print(comment.findall(text1))
print(comment.findall(text2))
#添加换行符
comment1 = re.compile(r'/\*((?:.|\n)*?)\*/')
print(comment1.findall(text2))
#re.DOTALL 使.可代表任何符号
comment2 = re.compile(r'/\*(.*?)\*/', re.DOTALL)
print(comment2.findall(text2))
2.9-2.10略
2.11从字符串中去掉不需要的字符
s = 'hello world \n'
#去除开始和结尾处的空白符
print(s.strip())
#去除左边的空白符
print(s.lstrip())
#去除右边的空白符
print(s.rstrip())
t = '-----hello===='
print(t.strip('-'))
#去掉两头可能出现的字符
print(t.strip('-='))
#对文本中间
#str.replace(), re.sub()
2.12略
2.13对齐文本字符串
text = 'Hello World'
print(text.ljust(20))
print(text.rjust(20))
print(text.center(20))
#可选的填充字符
print(text.ljust(20, '='))
#format()
#左对齐
print(format(text, '<20'))
#右对齐
print(format(text, '>20'))
#居中对齐
print(format(text, '^20'))
#左对齐填充字符
print(format(text, '=<20'))
print('{:>10s} {:>10s}'.format('Hello', 'World'))
x = 1.2345
print(format(x, '>10'))
print(format(x, '^10.2f'))
2.14字符串连接及合并
parts = ['Is', 'Chicago', 'Not', 'Chicago?']
print(' '.join(parts))
print(','.join(parts))
print(''.join(parts))
#+
a = 'Is Chicago'
b = 'Not Chicago?'
print(a+' '+b)
#format
print('{} {}'.format(a, b))
#排在一起
c = 'Hello' 'World'
print(c)
#生成器
data = ['ACME', 50, 91.1]
print(','.join(str(d) for d in data))
2.15给字符串中的变量名做插值处理
s = '{name} has {n} message'
print(s.format(name='Guido', n=37))
#要替换的值在变量中能找到
name = 'Guido'
n = 37
#vars()返回变量字典
print(s.format_map(vars()))
#vars()能作用在类实例上
class Info(object):
def __init__(self, name, n):
self.name = name
self.n = n
a = Info('Guido', 37)
print(s.format_map(vars(a)))
#处理缺少某个值的情况
class safesub(dict):
def __missing__(self, key):
return '{' + key + '}'
del n
print(s.format_map(safesub(vars())))
import sys
def sub(text):
return text.format_map(safesub(sys._getframe(1).f_locals))
n = 37
print(sub('Hello {name}'))
print(sub('You have {n} messages'))
print(sub('Your favorite color is {color}'))
#模板字符串
import string
s = string.Template('$name has $n messages.')
print(s.substitute(vars()))
2.16以固定的列数重新格式化文本
s = "Look into mu eyes, look into my eyes, the eyes, the eyes, the eyes, not around the eyes, don't look around the eyes, look into my eyes, you're under"
import textwrap
print(textwrap.fill(s, 70))
print(textwrap.fill(s, 40))
#第一行的缩进
print(textwrap.fill(s, 40, initial_indent=' '))
#除第一行后的缩进
print(textwrap.fill(s, 40, subsequent_indent=' '))
2.17-2.20略
本文深入探讨了使用正则表达式处理多行文本的方法,包括匹配、替换和提取特定模式。同时,介绍了如何利用Python内置函数进行字符串操作,如去除空白符、对齐文本、格式化字符串以及文本的重新格式化。
294





