龙空技术网

python中strip()的正则表达式版本

字母侠 108

前言:

当前小伙伴们对“python字符strip”大约比较着重,咱们都需要了解一些“python字符strip”的相关知识。那么小编在网摘上网罗了一些对于“python字符strip””的相关文章,希望小伙伴们能喜欢,各位老铁们一起来学习一下吧!

#coding=gbk

import re

def Strip(str1,str2=''):

if str2 == '':

print('原字符串:'+str1)

Regex1 = re.compile(r'^\s*|\s*$')

print(Regex1.sub('',str1))

else:

print('原字符串:'+str1)

Regex2 = re.compile(str2)

print(Regex2.sub('',str1))

Strip('sxtxr',r'x')

标签: #python字符strip